diff --git a/packages/nx/src/native/tests/watcher.spec.ts b/packages/nx/src/native/tests/watcher.spec.ts index f4065b1dfefdd2..07a0c515176b59 100644 --- a/packages/nx/src/native/tests/watcher.spec.ts +++ b/packages/nx/src/native/tests/watcher.spec.ts @@ -27,10 +27,13 @@ describe('watcher', () => { temp.cleanup(); }); - it('should trigger the callback for files that are not ignored', (done) => { - watcher = new Watcher(temp.tempDir); - watcher.watch((error, paths) => { - expect(paths).toMatchInlineSnapshot(` + it('should trigger the callback for files that are not ignored', async () => { + return new Promise(async (done) => { + await wait(); + + watcher = new Watcher(temp.tempDir); + watcher.watch((error, paths) => { + expect(paths).toMatchInlineSnapshot(` [ { "path": "app1/main.html", @@ -38,21 +41,25 @@ describe('watcher', () => { }, ] `); - done(); - }); + done(); + }); - wait().then(() => { + await wait(); temp.createFileSync('node_modules/my-file.json', JSON.stringify({})); + await wait(); temp.createFileSync('app2/main.css', JSON.stringify({})); + await wait(); temp.createFileSync('app1/main.html', JSON.stringify({})); }); - }); + }, 10000); - it('should trigger the callback when files are updated', (done) => { - watcher = new Watcher(temp.tempDir); + it('should trigger the callback when files are updated', async () => { + return new Promise(async (done) => { + await wait(); + watcher = new Watcher(temp.tempDir); - watcher.watch((err, paths) => { - expect(paths).toMatchInlineSnapshot(` + watcher.watch((err, paths) => { + expect(paths).toMatchInlineSnapshot(` [ { "path": "app1/main.js", @@ -60,46 +67,51 @@ describe('watcher', () => { }, ] `); - done(); - }); + done(); + }); - wait(1000).then(() => { + await wait(); // nxignored file should not trigger a callback temp.appendFile('app2/main.js', 'update'); + await wait(); temp.appendFile('app1/main.js', 'update'); }); - }); + }, 10000); - it('should watch file renames', (done) => { - watcher = new Watcher(temp.tempDir); + it('should watch file renames', async () => { + return new Promise(async (done) => { + await wait(); + watcher = new Watcher(temp.tempDir); - watcher.watch((err, paths) => { - expect(paths.length).toBe(2); - expect(paths.find((p) => p.type === 'create')).toMatchInlineSnapshot(` + watcher.watch((err, paths) => { + expect(paths.length).toBe(2); + expect(paths.find((p) => p.type === 'create')).toMatchInlineSnapshot(` { "path": "app1/rename.js", "type": "create", } `); - expect(paths.find((p) => p.type === 'delete')).toMatchInlineSnapshot(` + expect(paths.find((p) => p.type === 'delete')).toMatchInlineSnapshot(` { "path": "app1/main.js", "type": "delete", } `); - done(); - }); + done(); + }); - wait().then(() => { + await wait(); temp.renameFile('app1/main.js', 'app1/rename.js'); }); - }); + }, 10000); - it('should trigger on deletes', (done) => { - watcher = new Watcher(temp.tempDir); + it('should trigger on deletes', async () => { + return new Promise(async (done) => { + await wait(); + watcher = new Watcher(temp.tempDir); - watcher.watch((err, paths) => { - expect(paths).toMatchInlineSnapshot(` + watcher.watch((err, paths) => { + expect(paths).toMatchInlineSnapshot(` [ { "path": "app1/main.js", @@ -107,19 +119,22 @@ describe('watcher', () => { }, ] `); - done(); - }); + done(); + }); - wait().then(() => { + await wait(); temp.removeFileSync('app1/main.js'); }); - }); + }, 10000); + + it('should ignore nested gitignores', async () => { + return new Promise(async (done) => { + await wait(); - it('should ignore nested gitignores', (done) => { - watcher = new Watcher(temp.tempDir); + watcher = new Watcher(temp.tempDir); - watcher.watch((err, paths) => { - expect(paths).toMatchInlineSnapshot(` + watcher.watch((err, paths) => { + expect(paths).toMatchInlineSnapshot(` [ { "path": "boo.txt", @@ -127,30 +142,33 @@ describe('watcher', () => { }, ] `); - done(); - }); + done(); + }); - wait().then(() => { + await wait(); // should not be triggered temp.createFileSync('nested-ignore/hello1.txt', ''); + await wait(); temp.createFileSync('boo.txt', ''); }); - }); - - it('should include files that are negated in nxignore but are ignored in gitignore', (done) => { - watcher = new Watcher(temp.tempDir); - watcher.watch((err, paths) => { - expect(paths.some(({ path }) => path === '.env.local')).toBeTruthy(); - done(); - }); - - wait().then(() => { + }, 10000); + + it('should include files that are negated in nxignore but are ignored in gitignore', async () => { + return new Promise(async (done) => { + await wait(); + watcher = new Watcher(temp.tempDir); + watcher.watch((err, paths) => { + expect(paths.some(({ path }) => path === '.env.local')).toBeTruthy(); + done(); + }); + + await wait(2000); temp.appendFile('.env.local', 'hello'); }); - }); + }, 15000); }); -function wait(timeout = 500) { +function wait(timeout = 1000) { return new Promise((res) => { setTimeout(() => { res();