aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/installer/managers/npm_spec.lua
blob: e3a2bc769c859b87888c2dcc4b66cb7b038a826e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
local Result = require "mason-core.result"
local match = require "luassert.match"
local npm = require "mason-core.installer.managers.npm"
local spawn = require "mason-core.spawn"
local spy = require "luassert.spy"
local stub = require "luassert.stub"
local test_helpers = require "mason-test.helpers"

describe("npm manager", function()
    local snapshot

    before_each(function()
        snapshot = assert.snapshot()
    end)

    after_each(function()
        snapshot:revert()
    end)

    it("should init package.json", function()
        local ctx = test_helpers.create_context()
        stub(ctx.fs, "append_file")
        stub(spawn, "npm")
        spawn.npm.returns(Result.success {})
        spawn.npm.on_call_with({ "version", "--json" }).returns(Result.success {
            stdout = [[ { "npm": "8.1.0" } ]],
        })
        ctx:execute(function()
            npm.init()
        end)

        assert.spy(ctx.spawn.npm).was_called(1)
        assert.spy(ctx.spawn.npm).was_called_with {
            "init",
            "--yes",
            "--scope=mason",
        }
        assert.spy(ctx.fs.append_file).was_called(1)
        assert.spy(ctx.fs.append_file).was_called_with(match.is_ref(ctx.fs), ".npmrc", "\nglobal-style=true")
    end)

    it("should use install-strategy on npm >= 9", function()
        local ctx = test_helpers.create_context()
        stub(ctx.fs, "append_file")
        stub(spawn, "npm")
        spawn.npm.returns(Result.success {})
        spawn.npm.on_call_with({ "version", "--json" }).returns(Result.success {
            stdout = [[ { "npm": "9.1.0" } ]],
        })
        ctx:execute(function()
            npm.init()
        end)

        assert.spy(ctx.spawn.npm).was_called(1)
        assert.spy(ctx.spawn.npm).was_called_with {
            "init",
            "--yes",
            "--scope=mason",
        }
        assert.spy(ctx.fs.append_file).was_called_with(match.is_ref(ctx.fs), ".npmrc", "\ninstall-strategy=shallow")
    end)

    it("should install", function()
        local ctx = test_helpers.create_context()
        ctx:execute(function()
            npm.install("my-package", "1.0.0")
        end)

        assert.spy(ctx.spawn.npm).was_called(1)
        assert.spy(ctx.spawn.npm).was_called_with {
            "install",
            "my-package@1.0.0",
            vim.NIL, -- extra_packages
        }
    end)

    it("should install extra packages", function()
        local ctx = test_helpers.create_context()
        ctx:execute(function()
            npm.install("my-package", "1.0.0", {
                extra_packages = { "extra-package" },
            })
        end)

        assert.spy(ctx.spawn.npm).was_called(1)
        assert.spy(ctx.spawn.npm).was_called_with {
            "install",
            "my-package@1.0.0",
            { "extra-package" },
        }
    end)

    it("should write output", function()
        local ctx = test_helpers.create_context()
        spy.on(ctx.stdio_sink, "stdout")

        ctx:execute(function()
            npm.install("my-package", "1.0.0")
        end)

        assert
            .spy(ctx.stdio_sink.stdout)
            .was_called_with(match.is_ref(ctx.stdio_sink), "Installing npm package my-package@1.0.0…\n")
    end)
end)