-
Notifications
You must be signed in to change notification settings - Fork 1
/
containsCJS.spec.mjs
69 lines (61 loc) · 2.08 KB
/
containsCJS.spec.mjs
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
import assert from 'node:assert';
import { describe, it } from 'node:test';
import containsCJS from './containsCJS.mjs';
describe('containsCJS()', () => {
describe('exports', () => {
const matches = [
'exports.foo =',
'exports.foo=',
'exports[something]=',
'module.exports = chaiDom',
'Object.defineProperty(exports,',
'Object.assign(exports,',
];
it('should find assignments to `exports`', () => {
for (const match of matches) {
assert.ok(containsCJS(match), match);
}
});
it('should NOT find `exports` in comments', () => {
for (const match of matches) {
const singleLineComment = `// something ${match} else`;
assert.ok(containsCJS(singleLineComment) === false, singleLineComment);
const blockComment = `* something ${match} else`;
assert.ok(containsCJS(blockComment) === false, blockComment);
}
});
});
describe('require', () => {
const bareMatches = [
'require(foo)',
"require('foo')",
];
const matches = [
...bareMatches,
"const foo = require('foo')",
" require('foo')",
" require('foo')",
];
it('should find calls to `require()`', () => {
for (const match of matches) {
assert.ok(containsCJS(match), match);
}
});
it('should NOT find calls to methods named `require`', () => {
for (const bareMatch of bareMatches) {
const bareNonMatch = `bar.${bareMatch}`;
assert.ok(containsCJS(bareNonMatch) === false, bareNonMatch);
}
assert.ok(containsCJS("const foo = bar.require(foo)") === false);
assert.ok(containsCJS("const foo = bar.require('foo')") === false);
});
it('should NOT find `require` in comments', () => {
for (const match of matches) {
const singleLineComment = `// something ${match} else`;
assert.ok(containsCJS(singleLineComment) === false, singleLineComment);
const blockComment = `* something ${match} else`;
assert.ok(containsCJS(blockComment) === false, blockComment);
}
});
});
});