-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
esbuild.test.ts
88 lines (75 loc) · 2.74 KB
/
esbuild.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
import {
checkFilesDoNotExist,
checkFilesExist,
cleanupProject,
newProject,
readFile,
runCLI,
runCommand,
uniq,
updateFile,
updateProjectConfig,
} from '@nrwl/e2e/utils';
describe('EsBuild Plugin', () => {
let proj: string;
beforeEach(() => (proj = newProject()));
afterEach(() => cleanupProject());
it('should setup and build projects using build', async () => {
const myPkg = uniq('my-pkg');
runCLI(`generate @nrwl/js:lib ${myPkg} --bundler=esbuild`);
updateFile(`libs/${myPkg}/src/index.ts`, `console.log('Hello');\n`);
updateProjectConfig(myPkg, (json) => {
json.targets.build.options.assets = [`libs/${myPkg}/assets/*`];
return json;
});
updateFile(`libs/${myPkg}/assets/a.md`, 'file a');
updateFile(`libs/${myPkg}/assets/b.md`, 'file b');
runCLI(`build ${myPkg}`);
expect(runCommand(`node dist/libs/${myPkg}/index.js`)).toMatch(/Hello/);
// main field should be set correctly in package.json
checkFilesExist(`dist/libs/${myPkg}/package.json`);
expect(runCommand(`node dist/libs/${myPkg}`)).toMatch(/Hello/);
expect(readFile(`dist/libs/${myPkg}/assets/a.md`)).toMatch(/file a/);
expect(readFile(`dist/libs/${myPkg}/assets/b.md`)).toMatch(/file b/);
/* CJS format is not used by default, but passing --format=esm,cjs generates with it.
*/
checkFilesDoNotExist(`dist/libs/${myPkg}/index.cjs`);
runCLI(`build ${myPkg} --format=esm,cjs`);
checkFilesExist(`dist/libs/${myPkg}/index.cjs`);
/* Metafile is not generated by default, but passing --metafile generates it.
*/
checkFilesDoNotExist(`dist/libs/${myPkg}/meta.json`);
runCLI(`build ${myPkg} --metafile`);
checkFilesExist(`dist/libs/${myPkg}/meta.json`);
/* Type errors are turned on by default
*/
updateFile(
`libs/${myPkg}/src/index.ts`,
`
const x: number = 'a'; // type error
console.log('Bye');
`
);
expect(() => runCLI(`build ${myPkg}`)).toThrow();
expect(() => runCLI(`build ${myPkg} --skipTypeCheck`)).not.toThrow();
expect(runCommand(`node dist/libs/${myPkg}/index.js`)).toMatch(/Bye/);
}, 300_000);
it('should bundle in workspace libs', async () => {
const parentLib = uniq('parent-lib');
const childLib = uniq('child-lib');
runCLI(`generate @nrwl/js:lib ${parentLib} --bundler=esbuild`);
runCLI(`generate @nrwl/js:lib ${childLib} --buildable=false`);
updateFile(
`libs/${parentLib}/src/index.ts`,
`import '@${proj}/${childLib}';`
);
updateFile(
`libs/${childLib}/src/index.ts`,
`console.log('Hello from lib');\n`
);
runCLI(`build ${parentLib}`);
expect(runCommand(`node dist/libs/${parentLib}/index.js`)).toMatch(
/Hello from lib/
);
}, 300_000);
});