-
-
Notifications
You must be signed in to change notification settings - Fork 543
/
Copy pathesm-node.test.ts
108 lines (98 loc) · 3.15 KB
/
esm-node.test.ts
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
106
107
108
import * as path from 'path'
import { createTeardown } from 'fs-teardown'
import { installLibrary } from '../module-utils'
const fsMock = createTeardown({
rootDir: path.resolve(__dirname, 'node-esm-tests'),
paths: {
'package.json': JSON.stringify({ type: 'module' }),
},
})
beforeAll(async () => {
await fsMock.prepare()
await installLibrary(fsMock.resolve('.'))
})
afterAll(async () => {
await fsMock.cleanup()
})
it('runs in a ESM Node.js project', async () => {
await fsMock.create({
'resolve.mjs': `
console.log('msw:', await import.meta.resolve('msw'))
console.log('msw/node:', await import.meta.resolve('msw/node'))
console.log('msw/native:', await import.meta.resolve('msw/native'))
`,
'runtime.mjs': `
import { http } from 'msw'
import { setupServer } from 'msw/node'
const server = setupServer(
http.get('/resource', () => new Response())
)
console.log(typeof server.listen)
`,
})
const resolveStdio = await fsMock.exec(
/**
* @note Using the import meta resolve flag
* to enable the "import.meta.resolve" API to see
* what library imports resolve to in Node.js ESM.
*/
'node --experimental-import-meta-resolve ./resolve.mjs',
)
expect(resolveStdio.stderr).toBe('')
/**
* @todo Take these expected export paths from package.json.
* That should be the source of truth.
*/
expect(resolveStdio.stdout).toMatch(
/^msw: (.+?)\/node_modules\/msw\/lib\/core\/index\.mjs/m,
)
expect(resolveStdio.stdout).toMatch(
/^msw\/node: (.+?)\/node_modules\/msw\/lib\/node\/index\.mjs/m,
)
expect(resolveStdio.stdout).toMatch(
/^msw\/native: (.+?)\/node_modules\/msw\/lib\/native\/index\.mjs/m,
)
/**
* @todo Also test the "msw/browser" import that throws,
* saying that the "./browser" export is not defined.
* That's correct, it's exlpicitly set as "browser: null" for Node.js.
*/
const runtimeStdio = await fsMock.exec('node ./runtime.mjs')
expect(runtimeStdio.stderr).toBe('')
expect(runtimeStdio.stdout).toMatch(/function/m)
})
it('runs in a CJS Node.js project', async () => {
await fsMock.create({
'resolve.cjs': `
console.log('msw:', require.resolve('msw'))
console.log('msw/node:', require.resolve('msw/node'))
console.log('msw/native:', require.resolve('msw/native'))
`,
'runtime.cjs': `
const { http } = require('msw')
const { setupServer } = require('msw/node')
const server = setupServer(
http.get('/resource', () => new Response())
)
console.log(typeof server.listen)
`,
})
const resolveStdio = await fsMock.exec('node ./resolve.cjs')
expect(resolveStdio.stderr).toBe('')
/**
* @todo Take these expected export paths from package.json.
* That should be the source of truth.
*/
expect(resolveStdio.stdout).toMatch(
/^msw: (.+?)\/node_modules\/msw\/lib\/core\/index\.js/m,
)
expect(resolveStdio.stdout).toMatch(
/^msw\/node: (.+?)\/node_modules\/msw\/lib\/node\/index\.js/m,
)
expect(resolveStdio.stdout).toMatch(
/^msw\/native: (.+?)\/node_modules\/msw\/lib\/native\/index\.js/m,
)
const runtimeStdio = await fsMock.exec('node ./runtime.mjs')
expect(runtimeStdio.stderr).toBe('')
expect(runtimeStdio.stdout).toMatch(/function/m)
})