|
| 1 | +// How `parseModuleMockSpecifiers` resolves the receiver of a mock-family call: the global |
| 2 | +// `vi`/`vitest`, or a local name an `import ... from 'vitest'` declaration binds to them. |
| 3 | + |
| 4 | +import assert from 'node:assert/strict'; |
| 5 | +import { test } from 'node:test'; |
| 6 | +import { parseModuleMockSpecifiers } from './model.ts'; |
| 7 | + |
| 8 | +function specs(...lines: string[]): string[] { |
| 9 | + return parseModuleMockSpecifiers(lines.join('\n')).map(({ spec }) => spec); |
| 10 | +} |
| 11 | + |
| 12 | +test('a named import aliasing vi or vitest is a mock receiver', () => { |
| 13 | + assert.deepEqual( |
| 14 | + specs( |
| 15 | + "import { vi as mocks, vitest as runner } from 'vitest';", |
| 16 | + "mocks.mock('a');", |
| 17 | + "runner.doMock('b');", |
| 18 | + "mocks['unmock']('c');", |
| 19 | + "await mocks.importActual('d');", |
| 20 | + "mocks?.importMock('e');", |
| 21 | + "(mocks as typeof vi).mock('f');", |
| 22 | + "mocks!.doUnmock('g');", |
| 23 | + ), |
| 24 | + ['a', 'b', 'c', 'd', 'e', 'f', 'g'], |
| 25 | + ); |
| 26 | +}); |
| 27 | + |
| 28 | +test('a string-named import and an unaliased import resolve like a named alias', () => { |
| 29 | + assert.deepEqual( |
| 30 | + specs("import { 'vi' as quoted, vi } from 'vitest';", "quoted.mock('a');", "vi.mock('b');"), |
| 31 | + ['a', 'b'], |
| 32 | + ); |
| 33 | +}); |
| 34 | + |
| 35 | +test('a vitest namespace import reaches the mock API through vi and vitest members', () => { |
| 36 | + assert.deepEqual( |
| 37 | + specs( |
| 38 | + "import * as testing from 'vitest';", |
| 39 | + "testing.vi.mock('a');", |
| 40 | + "testing.vitest.doMock('b');", |
| 41 | + "testing['vi'].unmock('c');", |
| 42 | + "testing.vi['importActual']('d');", |
| 43 | + "testing[`vitest`].importMock('e');", |
| 44 | + "testing?.vi?.mock('f');", |
| 45 | + "(testing.vi as typeof vi).mock('g');", |
| 46 | + ), |
| 47 | + ['a', 'b', 'c', 'd', 'e', 'f', 'g'], |
| 48 | + ); |
| 49 | +}); |
| 50 | + |
| 51 | +test('only a value import from vitest binds a mock receiver', () => { |
| 52 | + assert.deepEqual( |
| 53 | + specs( |
| 54 | + "import { vi as fromOther } from 'not-vitest';", |
| 55 | + "import { vi as fromRelative } from './vitest.ts';", |
| 56 | + "import type { vi as typeDeclaration } from 'vitest';", |
| 57 | + "import { type vi as typeSpecifier } from 'vitest';", |
| 58 | + "import { expect as notMock, describe as alsoNotMock } from 'vitest';", |
| 59 | + "import * as otherNamespace from 'not-vitest';", |
| 60 | + "import type * as typeNamespace from 'vitest';", |
| 61 | + "fromOther.mock('a');", |
| 62 | + "fromRelative.mock('b');", |
| 63 | + "typeDeclaration.mock('c');", |
| 64 | + "typeSpecifier.mock('d');", |
| 65 | + "notMock.mock('e');", |
| 66 | + "alsoNotMock.mock('f');", |
| 67 | + "otherNamespace.vi.mock('g');", |
| 68 | + "typeNamespace.vi.mock('h');", |
| 69 | + ), |
| 70 | + [], |
| 71 | + ); |
| 72 | +}); |
| 73 | + |
| 74 | +test('a vitest namespace member other than vi or vitest is not the mock API', () => { |
| 75 | + assert.deepEqual( |
| 76 | + specs( |
| 77 | + "import * as testing from 'vitest';", |
| 78 | + "testing.expect.mock('a');", |
| 79 | + "testing[member].mock('b');", |
| 80 | + "testing['expect'].mock('c');", |
| 81 | + ), |
| 82 | + [], |
| 83 | + ); |
| 84 | +}); |
| 85 | + |
| 86 | +test('an alias is not a mock receiver where an enclosing scope redeclares its name', () => { |
| 87 | + assert.deepEqual( |
| 88 | + specs( |
| 89 | + "import { vi as mocks } from 'vitest';", |
| 90 | + "import * as testing from 'vitest';", |
| 91 | + "function parameter(mocks) { mocks.mock('a'); }", |
| 92 | + "const arrow = (testing) => testing.vi.mock('b');", |
| 93 | + "{ const mocks = other; mocks.mock('c'); }", |
| 94 | + "function hoisted() { if (flag) { var mocks = other; } mocks.mock('d'); }", |
| 95 | + "try {} catch (mocks) { mocks.mock('e'); }", |
| 96 | + "for (const { mocks } of list) mocks.mock('f');", |
| 97 | + "switch (kind) { case 1: class testing {} testing.vi.mock('g'); }", |
| 98 | + "const expression = function mocks() { mocks.mock('h'); };", |
| 99 | + "const named = class mocks { run() { mocks.mock('i'); } };", |
| 100 | + "namespace Fixture { const testing = other; testing.vi.mock('j'); }", |
| 101 | + ), |
| 102 | + [], |
| 103 | + ); |
| 104 | + assert.deepEqual( |
| 105 | + specs( |
| 106 | + "import { vi as mocks } from 'vitest';", |
| 107 | + 'function unrelated(mocks) { return mocks; }', |
| 108 | + '{ const mocks = other; }', |
| 109 | + "mocks.mock('outside');", |
| 110 | + ), |
| 111 | + ['outside'], |
| 112 | + ); |
| 113 | +}); |
| 114 | + |
| 115 | +test('the global vi and vitest names stay mock receivers beside import aliases', () => { |
| 116 | + assert.deepEqual( |
| 117 | + specs( |
| 118 | + "import { vi as mocks } from 'vitest';", |
| 119 | + "vi.mock('a');", |
| 120 | + "vitest.mock('b');", |
| 121 | + "async function setup() { const { vi } = await import('vitest'); vi.doMock('c'); }", |
| 122 | + ), |
| 123 | + ['a', 'b', 'c'], |
| 124 | + ); |
| 125 | +}); |
| 126 | + |
| 127 | +test('an alias call still ignores comments, non-literal modules and non-module methods', () => { |
| 128 | + assert.deepEqual( |
| 129 | + specs( |
| 130 | + "import { vi as mocks } from 'vitest';", |
| 131 | + "import * as testing from 'vitest';", |
| 132 | + "// mocks.mock('commented');", |
| 133 | + "/* testing.vi.doMock('block') */", |
| 134 | + 'const text = "mocks.mock(\'in-string\')";', |
| 135 | + 'mocks.mock(variable);', |
| 136 | + 'mocks.mock(`interpolated-${name}`);', |
| 137 | + 'testing.vi.mock();', |
| 138 | + "mocks.fn('not-a-module-api');", |
| 139 | + "mocks.mocked('not-a-module-api');", |
| 140 | + "mocks[method]('computed-member');", |
| 141 | + "mocks.mock.mockReset('nested');", |
| 142 | + "mocks('call-not-member');", |
| 143 | + ), |
| 144 | + [], |
| 145 | + ); |
| 146 | +}); |
| 147 | + |
| 148 | +test('a Vitest binding made by a dynamic import is not followed', () => { |
| 149 | + assert.deepEqual( |
| 150 | + specs( |
| 151 | + "const { vi: mocks } = await import('vitest');", |
| 152 | + "const testing = await import('vitest');", |
| 153 | + "mocks.mock('a');", |
| 154 | + "testing.vi.mock('b');", |
| 155 | + ), |
| 156 | + [], |
| 157 | + ); |
| 158 | +}); |
| 159 | + |
| 160 | +test('a module named by an alias call keeps its source line', () => { |
| 161 | + assert.deepEqual( |
| 162 | + parseModuleMockSpecifiers( |
| 163 | + "import { vi as mocks } from 'vitest';\n\nmocks.mock(\n `multi`,\n);", |
| 164 | + ), |
| 165 | + [{ spec: 'multi', line: 3 }], |
| 166 | + ); |
| 167 | +}); |
0 commit comments