|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { UnwatchedDir } from 'broccoli-source'; |
| 4 | +import sideWatch from '../src'; |
| 5 | +import { Project } from 'scenario-tester'; |
| 6 | +import { join } from 'path'; |
| 7 | +import { createBuilder } from 'broccoli-test-helper'; |
| 8 | + |
| 9 | +async function generateProject() { |
| 10 | + const project = new Project('my-app', { |
| 11 | + files: { |
| 12 | + src: { |
| 13 | + 'index.js': 'export default 123', |
| 14 | + }, |
| 15 | + other: { |
| 16 | + 'index.js': 'export default 456;', |
| 17 | + }, |
| 18 | + }, |
| 19 | + }); |
| 20 | + |
| 21 | + await project.write(); |
| 22 | + |
| 23 | + return project; |
| 24 | +} |
| 25 | + |
| 26 | +describe('broccoli-side-watch', function () { |
| 27 | + test('it returns existing tree without options', async function () { |
| 28 | + const project = await generateProject(); |
| 29 | + const existingTree = new UnwatchedDir(join(project.baseDir, 'src')); |
| 30 | + |
| 31 | + const node = sideWatch(existingTree); |
| 32 | + |
| 33 | + expect(node).toEqual(existingTree); |
| 34 | + }); |
| 35 | + |
| 36 | + test('it watches additional relative paths', async function () { |
| 37 | + const project = await generateProject(); |
| 38 | + const existingTree = new UnwatchedDir(join(project.baseDir, 'src')); |
| 39 | + |
| 40 | + const node = sideWatch(existingTree, { watching: ['./other'], cwd: project.baseDir }); |
| 41 | + const output = createBuilder(node); |
| 42 | + await output.build(); |
| 43 | + |
| 44 | + expect(output.read()).toEqual({ 'index.js': 'export default 123' }); |
| 45 | + |
| 46 | + const watchedNode = node |
| 47 | + .__broccoliGetInfo__() |
| 48 | + .inputNodes[1].__broccoliGetInfo__() |
| 49 | + .inputNodes[0].__broccoliGetInfo__(); |
| 50 | + expect(watchedNode).toHaveProperty('watched', true); |
| 51 | + expect(watchedNode).toHaveProperty('sourceDirectory', join(project.baseDir, 'other')); |
| 52 | + }); |
| 53 | + |
| 54 | + test('it watches additional absolute paths', async function () { |
| 55 | + const project = await generateProject(); |
| 56 | + const existingTree = new UnwatchedDir(join(project.baseDir, 'src')); |
| 57 | + |
| 58 | + const node = sideWatch(existingTree, { watching: [join(project.baseDir, './other')] }); |
| 59 | + const output = createBuilder(node); |
| 60 | + await output.build(); |
| 61 | + |
| 62 | + expect(output.read()).toEqual({ 'index.js': 'export default 123' }); |
| 63 | + |
| 64 | + const watchedNode = node |
| 65 | + .__broccoliGetInfo__() |
| 66 | + .inputNodes[1].__broccoliGetInfo__() |
| 67 | + .inputNodes[0].__broccoliGetInfo__(); |
| 68 | + expect(watchedNode).toHaveProperty('watched', true); |
| 69 | + expect(watchedNode).toHaveProperty('sourceDirectory', join(project.baseDir, 'other')); |
| 70 | + }); |
| 71 | + |
| 72 | + test('it watches additional package', async function () { |
| 73 | + const project = await generateProject(); |
| 74 | + project.addDependency( |
| 75 | + new Project('some-dep', '0.0.0', { |
| 76 | + files: { |
| 77 | + 'index.js': `export default 'some';`, |
| 78 | + }, |
| 79 | + }) |
| 80 | + ); |
| 81 | + await project.write(); |
| 82 | + |
| 83 | + const existingTree = new UnwatchedDir(join(project.baseDir, 'src')); |
| 84 | + |
| 85 | + const node = sideWatch(existingTree, { watching: ['some-dep'], cwd: project.baseDir }); |
| 86 | + const output = createBuilder(node); |
| 87 | + await output.build(); |
| 88 | + |
| 89 | + expect(output.read()).toEqual({ 'index.js': 'export default 123' }); |
| 90 | + |
| 91 | + const watchedNode = node |
| 92 | + .__broccoliGetInfo__() |
| 93 | + .inputNodes[1].__broccoliGetInfo__() |
| 94 | + .inputNodes[0].__broccoliGetInfo__(); |
| 95 | + expect(watchedNode).toHaveProperty('watched', true); |
| 96 | + expect(watchedNode).toHaveProperty('sourceDirectory', join(project.baseDir, 'node_modules/some-dep')); |
| 97 | + }); |
| 98 | + |
| 99 | + test('it watches additional package with exports', async function () { |
| 100 | + const project = await generateProject(); |
| 101 | + project.addDependency( |
| 102 | + new Project('some-dep', '0.0.0', { |
| 103 | + files: { |
| 104 | + 'package.json': JSON.stringify({ |
| 105 | + exports: { |
| 106 | + './*': { |
| 107 | + types: './declarations/*.d.ts', |
| 108 | + default: './dist/*.js', |
| 109 | + }, |
| 110 | + }, |
| 111 | + }), |
| 112 | + src: { |
| 113 | + 'index.ts': `export default 'some';`, |
| 114 | + }, |
| 115 | + dist: { |
| 116 | + 'index.js': `export default 'some';`, |
| 117 | + }, |
| 118 | + declarations: { |
| 119 | + 'index.d.ts': `export default 'some';`, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }) |
| 123 | + ); |
| 124 | + await project.write(); |
| 125 | + |
| 126 | + const existingTree = new UnwatchedDir(join(project.baseDir, 'src')); |
| 127 | + |
| 128 | + const node = sideWatch(existingTree, { watching: ['some-dep'], cwd: project.baseDir }); |
| 129 | + const output = createBuilder(node); |
| 130 | + await output.build(); |
| 131 | + |
| 132 | + expect(output.read()).toEqual({ 'index.js': 'export default 123' }); |
| 133 | + |
| 134 | + const watchedNode = node |
| 135 | + .__broccoliGetInfo__() |
| 136 | + .inputNodes[1].__broccoliGetInfo__() |
| 137 | + .inputNodes[0].__broccoliGetInfo__(); |
| 138 | + expect(watchedNode).toHaveProperty('watched', true); |
| 139 | + expect(watchedNode).toHaveProperty('sourceDirectory', join(project.baseDir, 'node_modules/some-dep/dist')); |
| 140 | + }); |
| 141 | +}); |
0 commit comments