1 | const {analyzeDependencies, isUpToDate} = require('../analyze-dependencies')
|
2 | const {resolve} = require('path')
|
3 | const {close, open} = require('fs-extra')
|
4 |
|
5 | const res = f => resolve(__dirname, '../testData', f);
|
6 |
|
7 | const resObject = (obj) => {
|
8 | const result = {};
|
9 |
|
10 | for (const key in obj) {
|
11 | result[res(key)] = obj[key].map(res);
|
12 | }
|
13 |
|
14 | return result;
|
15 | }
|
16 |
|
17 | const fsTouch = filename => open(filename, 'w').then(close);
|
18 |
|
19 | describe('analyze-dependencies', () => {
|
20 | it('should read cjs', () => {
|
21 | const deps = analyzeDependencies(res('cjs/a.carmi.js'))
|
22 | expect(deps).toEqual(resObject({
|
23 | 'cjs/a.carmi.js': ['cjs/b.carmi.js'],
|
24 | 'cjs/b.carmi.js': ['cjs/c.carmi.js'],
|
25 | 'cjs/c.carmi.js': []
|
26 | }))
|
27 | })
|
28 |
|
29 | it('should read ts', () => {
|
30 | const deps = analyzeDependencies(res('ts/a.carmi.js'))
|
31 | expect(deps).toEqual(resObject({
|
32 | 'ts/a.carmi.js': ['ts/b.carmi.ts'],
|
33 | 'ts/b.carmi.ts': ['ts/c.carmi.js'],
|
34 | 'ts/c.carmi.js': []
|
35 | }))
|
36 | })
|
37 |
|
38 | it('should read ts from package.json main field', async () => {
|
39 | const deps = analyzeDependencies(res('ts/e.carmi.ts'), null)
|
40 | expect(deps).toEqual(resObject({
|
41 | 'ts/e.carmi.ts': ['ts/d/main.carmi.ts'],
|
42 | 'ts/d/main.carmi.ts': ['ts/d/f.carmi.js'],
|
43 | 'ts/d/f.carmi.js': []
|
44 | }))
|
45 | })
|
46 |
|
47 | it('should read esm', () => {
|
48 | const deps = analyzeDependencies(res('esm/a.carmi.js'))
|
49 | expect(deps).toEqual(resObject({
|
50 | 'esm/a.carmi.js': ['esm/b.carmi.js'],
|
51 | 'esm/b.carmi.js': ['esm/c.carmi.js'],
|
52 | 'esm/c.carmi.js': []
|
53 | }))
|
54 | })
|
55 |
|
56 | it('should isUpToDate when output is new', async () => {
|
57 | await fsTouch(res('esm/a.output.js'))
|
58 | const deps = analyzeDependencies(res('esm/a.carmi.js'))
|
59 | const upToDate = isUpToDate(deps, res('esm/a.output.js'))
|
60 | expect(upToDate).toEqual(true)
|
61 | })
|
62 |
|
63 | it('should isUpToDate when input is new', async () => {
|
64 | await fsTouch(res('esm/c.carmi.js'))
|
65 |
|
66 | const deps = res('esm/a.carmi.js')
|
67 | const upToDate = isUpToDate(deps, res('esm/a.output.js'))
|
68 | expect(upToDate).toEqual(false)
|
69 | })
|
70 | })
|