Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 22d5576

Browse files
test: improve assertions (#51)
1 parent 90fdc58 commit 22d5576

File tree

8 files changed

+50
-48
lines changed

8 files changed

+50
-48
lines changed

tests/specs/javascript/cjs.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { testSuite, expect } from 'manten';
22
import semver from 'semver';
33
import type { NodeApis } from '../../utils/node-with-loader';
44
import nodeSupports from '../../utils/node-supports';
5+
import { assertNotFound } from '../../utils/assertions';
56

67
export default testSuite(async ({ describe }, node: NodeApis) => {
78
describe('Load CJS', ({ describe }) => {
@@ -51,12 +52,12 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
5152

5253
test('Load', async () => {
5354
const nodeProcess = await node.load(importPath);
54-
expect(nodeProcess.stderr).toMatch('Cannot find module');
55+
assertNotFound(nodeProcess.stderr, importPath);
5556
});
5657

5758
test('Import', async () => {
5859
const nodeProcess = await node.import(importPath);
59-
expect(nodeProcess.stderr).toMatch('Cannot find module');
60+
assertNotFound(nodeProcess.stderr, importPath);
6061
});
6162
});
6263

@@ -65,12 +66,12 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
6566

6667
test('Load', async () => {
6768
const nodeProcess = await node.load(importPath);
68-
expect(nodeProcess.stderr).toMatch('Error');
69+
assertNotFound(nodeProcess.stderr, importPath);
6970
});
7071

7172
test('Import', async () => {
7273
const nodeProcess = await node.import(importPath);
73-
expect(nodeProcess.stderr).toMatch('Error');
74+
assertNotFound(nodeProcess.stderr, importPath);
7475
});
7576
});
7677
});

tests/specs/javascript/esm.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { testSuite, expect } from 'manten';
22
import semver from 'semver';
33
import type { NodeApis } from '../../utils/node-with-loader';
44
import nodeSupports from '../../utils/node-supports';
5+
import { assertNotFound } from '../../utils/assertions';
56

67
export default testSuite(async ({ describe }, node: NodeApis) => {
78
describe('Load ESM', ({ describe }) => {
@@ -47,12 +48,12 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
4748

4849
test('Load', async () => {
4950
const nodeProcess = await node.load(importPath);
50-
expect(nodeProcess.stderr).toMatch('Cannot find module');
51+
assertNotFound(nodeProcess.stderr, importPath);
5152
});
5253

5354
test('Import', async () => {
5455
const nodeProcess = await node.import(importPath);
55-
expect(nodeProcess.stderr).toMatch('Cannot find module');
56+
assertNotFound(nodeProcess.stderr, importPath);
5657
});
5758
});
5859

@@ -61,12 +62,12 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
6162

6263
test('Load', async () => {
6364
const nodeProcess = await node.load(importPath);
64-
expect(nodeProcess.stderr).toMatch('Cannot find module');
65+
assertNotFound(nodeProcess.stderr, importPath);
6566
});
6667

6768
test('Import', async () => {
6869
const nodeProcess = await node.import(importPath);
69-
expect(nodeProcess.stderr).toMatch('Cannot find module');
70+
assertNotFound(nodeProcess.stderr, importPath);
7071
});
7172
});
7273
});
@@ -152,7 +153,7 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
152153

153154
test('Import', async () => {
154155
const nodeProcess = await node.import(importPath);
155-
expect(nodeProcess.stderr).toMatch('ERR_MODULE_NOT_FOUND');
156+
assertNotFound(nodeProcess.stderr, importPath);
156157
});
157158
});
158159
});

tests/specs/typescript/cts.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { testSuite, expect } from 'manten';
22
import type { NodeApis } from '../../utils/node-with-loader';
3-
4-
const isWin = process.platform === 'win32';
3+
import { assertNotFound } from '../../utils/assertions';
54

65
export default testSuite(async ({ describe }, node: NodeApis) => {
76
describe('.cts extension', ({ describe }) => {
@@ -30,7 +29,7 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
3029

3130
test('Load - should not work', async () => {
3231
const nodeProcess = await node.load(importPath);
33-
expect(nodeProcess.stderr).toMatch('Cannot find module');
32+
assertNotFound(nodeProcess.stderr, importPath);
3433
});
3534

3635
test('Import', async () => {
@@ -44,17 +43,12 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
4443

4544
test('Load', async () => {
4645
const nodeProcess = await node.load(importPath);
47-
expect(nodeProcess.stderr).toMatch('Cannot find module');
46+
assertNotFound(nodeProcess.stderr, importPath);
4847
});
4948

5049
test('Import', async () => {
5150
const nodeProcess = await node.import(importPath);
52-
expect(nodeProcess.stderr).toMatch('Cannot find module');
53-
expect(nodeProcess.stderr).toMatch(
54-
isWin
55-
? '\\lib\\ts-ext-cts\\index\''
56-
: '/lib/ts-ext-cts/index\'',
57-
);
51+
assertNotFound(nodeProcess.stderr, importPath);
5852
});
5953
});
6054

@@ -63,17 +57,12 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
6357

6458
test('Load', async () => {
6559
const nodeProcess = await node.load(importPath);
66-
expect(nodeProcess.stderr).toMatch('Cannot find module');
60+
assertNotFound(nodeProcess.stderr, importPath);
6761
});
6862

6963
test('Import', async () => {
7064
const nodeProcess = await node.import(importPath);
71-
expect(nodeProcess.stderr).toMatch('Cannot find module');
72-
expect(nodeProcess.stderr).toMatch(
73-
isWin
74-
? '\\lib\\ts-ext-cts\''
75-
: '/lib/ts-ext-cts\'',
76-
);
65+
assertNotFound(nodeProcess.stderr, importPath);
7766
});
7867
});
7968
});

tests/specs/typescript/jsx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { testSuite, expect } from 'manten';
22
import semver from 'semver';
33
import type { NodeApis } from '../../utils/node-with-loader';
44
import nodeSupports from '../../utils/node-supports';
5+
import { assertNotFound } from '../../utils/assertions';
56

67
export default testSuite(async ({ describe }, node: NodeApis) => {
78
describe('.jsx extension', ({ describe }) => {
@@ -84,7 +85,7 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
8485

8586
test('Import', async () => {
8687
const nodeProcess = await node.import(importPath);
87-
expect(nodeProcess.stderr).toMatch('ERR_MODULE_NOT_FOUND');
88+
assertNotFound(nodeProcess.stderr, importPath);
8889
});
8990
});
9091
});

tests/specs/typescript/mts.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { testSuite, expect } from 'manten';
22
import semver from 'semver';
33
import type { NodeApis } from '../../utils/node-with-loader';
44
import nodeSupports from '../../utils/node-supports';
5-
6-
const isWin = process.platform === 'win32';
5+
import { assertNotFound } from '../../utils/assertions';
76

87
export default testSuite(async ({ describe }, node: NodeApis) => {
98
describe('.mts extension', ({ describe }) => {
@@ -41,7 +40,7 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
4140

4241
test('Load - should not work', async () => {
4342
const nodeProcess = await node.load(importPath);
44-
expect(nodeProcess.stderr).toMatch('Cannot find module');
43+
assertNotFound(nodeProcess.stderr, importPath);
4544
});
4645

4746
test('Import', async () => {
@@ -56,36 +55,26 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
5655

5756
test('Load', async () => {
5857
const nodeProcess = await node.load(importPath);
59-
expect(nodeProcess.stderr).toMatch('Cannot find module');
58+
assertNotFound(nodeProcess.stderr, importPath);
6059
});
6160

6261
test('Import', async () => {
6362
const nodeProcess = await node.import(importPath);
64-
expect(nodeProcess.stderr).toMatch('Cannot find module');
65-
expect(nodeProcess.stderr).toMatch(
66-
isWin
67-
? '\\lib\\ts-ext-mts\\index\''
68-
: '/lib/ts-ext-mts/index\'',
69-
);
63+
assertNotFound(nodeProcess.stderr, importPath);
7064
});
7165
});
7266

7367
describe('directory - should not work', ({ test }) => {
74-
const importPath = './lib/ts-ext-mts/';
68+
const importPath = './lib/ts-ext-mts';
7569

7670
test('Load', async () => {
7771
const nodeProcess = await node.load(importPath);
78-
expect(nodeProcess.stderr).toMatch('Cannot find module');
72+
assertNotFound(nodeProcess.stderr, importPath);
7973
});
8074

8175
test('Import', async () => {
8276
const nodeProcess = await node.import(importPath);
83-
expect(nodeProcess.stderr).toMatch('Cannot find module');
84-
expect(nodeProcess.stderr).toMatch(
85-
isWin
86-
? '\\lib\\ts-ext-mts\\\''
87-
: '/lib/ts-ext-mts/\'',
88-
);
77+
assertNotFound(nodeProcess.stderr, importPath);
8978
});
9079
});
9180
});

tests/specs/typescript/ts.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { testSuite, expect } from 'manten';
22
import semver from 'semver';
33
import type { NodeApis } from '../../utils/node-with-loader';
44
import nodeSupports from '../../utils/node-supports';
5+
import { assertNotFound } from '../../utils/assertions';
56

67
export default testSuite(async ({ describe }, node: NodeApis) => {
78
describe('.ts extension', ({ describe }) => {
@@ -49,7 +50,7 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
4950

5051
test('Load - should not work', async () => {
5152
const nodeProcess = await node.load(importPath);
52-
expect(nodeProcess.stderr).toMatch('Cannot find module');
53+
assertNotFound(nodeProcess.stderr, importPath);
5354
});
5455

5556
test('Import', async () => {
@@ -124,7 +125,7 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
124125

125126
test('Import', async () => {
126127
const nodeProcess = await node.import(importPath);
127-
expect(nodeProcess.stderr).toMatch('ERR_MODULE_NOT_FOUND');
128+
assertNotFound(nodeProcess.stderr, importPath);
128129
});
129130
});
130131
});

tests/specs/typescript/tsx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { testSuite, expect } from 'manten';
22
import semver from 'semver';
33
import type { NodeApis } from '../../utils/node-with-loader';
44
import nodeSupports from '../../utils/node-supports';
5+
import { assertNotFound } from '../../utils/assertions';
56

67
export default testSuite(async ({ describe }, node: NodeApis) => {
78
describe('.tsx extension', ({ describe }) => {
@@ -84,7 +85,7 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
8485

8586
test('Import', async () => {
8687
const nodeProcess = await node.import(importPath);
87-
expect(nodeProcess.stderr).toMatch('ERR_MODULE_NOT_FOUND');
88+
assertNotFound(nodeProcess.stderr, importPath);
8889
});
8990
});
9091
});

tests/utils/assertions.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { expect } from 'manten';
2+
3+
const isWin = process.platform === 'win32';
4+
5+
const agnosticPath = (path: string) => (
6+
isWin
7+
? path.replace(/\//g, '\\')
8+
: path
9+
);
10+
11+
export const assertNotFound = (
12+
stderr: string,
13+
modulePath: string,
14+
) => {
15+
expect(stderr).toMatch('ERR_MODULE_NOT_FOUND');
16+
17+
const nonRelativePath = modulePath.startsWith('.') ? modulePath.slice(1) : modulePath;
18+
expect(stderr).toMatch(agnosticPath(`${nonRelativePath}'`));
19+
};

0 commit comments

Comments
 (0)