-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathesmock.node.global.test.js
More file actions
77 lines (63 loc) · 2.39 KB
/
esmock.node.global.test.js
File metadata and controls
77 lines (63 loc) · 2.39 KB
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
import test from 'node:test'
import assert from 'node:assert/strict'
import esmock from 'esmock'
test('should import mock objects, local', async () => {
const hostObjects = await esmock('../local/usesImportObjects.js', {
'../local/exampleMJS.mjs': () => 'second mocked',
import: {
Date: { now: () => 123456789012 },
setTimeout: fn => fn(),
fetch: () => ({ res: 404 })
}
})
assert.strictEqual(hostObjects.dateWrapper.now(), 123456789012)
assert.strictEqual(hostObjects.setTimeoutWrapper(() => ':)'), ':)')
assert.strictEqual(hostObjects.fetchWrapper().res, 404)
assert.notStrictEqual(hostObjects.child.dateWrapper.now(), 123456789098)
assert.notStrictEqual(hostObjects.child.setTimeoutWrapper(() => ':)'), ':)')
assert.notStrictEqual(hostObjects.child.otherimport(), 'other import')
})
test('should import mock objects, global', async () => {
const hostObjects = await esmock('../local/usesImportObjects.js', {}, {
'../local/exampleMJS.mjs': () => 'other import',
import: {
Date: { now: () => 123456789098 },
setTimeout: fn => fn(),
fetch: () => ({ res: 404 })
}
})
assert.strictEqual(hostObjects.dateWrapper.now(), 123456789098)
assert.strictEqual(hostObjects.setTimeoutWrapper(() => ':)'), ':)')
assert.strictEqual(hostObjects.fetchWrapper().res, 404)
assert.strictEqual(hostObjects.child.dateWrapper.now(), 123456789098)
assert.strictEqual(hostObjects.child.setTimeoutWrapper(() => ':)'), ':)')
assert.strictEqual(hostObjects.child.fetchWrapper().res, 404)
assert.strictEqual(hostObjects.child.otherimport(), 'other import')
})
test('should mock fetch as shown in README', async () => {
const reqUsers = await esmock('../local/usesImportObjects.js', {
import: {
fetch: () => '[["jim😄",1],["jen😊",2}]'
}
})
assert.strictEqual(await reqUsers(), '[["jim😄",1],["jen😊",2}]')
})
test('should mock files with hashbangs', async () => {
const logs = []
await esmock('../local/hashbang.js', {
'../local/env.js': { TESTCONSTANT: 'foo' },
import: {
console: { log: () => logs.push('foo') }
}
})
assert.deepEqual(logs, ['foo'])
})
test('should work when modules have CJS imports', async () => {
const logs = []
await esmock('../local/usesModuleWithCJSDependency.js', {}, {
import: {
console: { log: () => logs.push('foo') }
}
})
assert.ok(logs.some(n => n === 'foo'))
})