Skip to content

Commit

Permalink
fix(core): stabilize watcher unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli committed Nov 7, 2023
1 parent 457bbcb commit e496d2d
Showing 1 changed file with 71 additions and 53 deletions.
124 changes: 71 additions & 53 deletions packages/nx/src/native/tests/watcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,130 +27,148 @@ 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<void>(async (done) => {
await wait();

watcher = new Watcher(temp.tempDir);
watcher.watch((error, paths) => {
expect(paths).toMatchInlineSnapshot(`
[
{
"path": "app1/main.html",
"type": "create",
},
]
`);
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<void>(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",
"type": "update",
},
]
`);
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<void>(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<void>(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",
"type": "delete",
},
]
`);
done();
});
done();
});

wait().then(() => {
await wait();
temp.removeFileSync('app1/main.js');
});
});
}, 10000);

it('should ignore nested gitignores', async () => {
return new Promise<void>(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",
"type": "create",
},
]
`);
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<void>(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<void>((res) => {
setTimeout(() => {
res();
Expand Down

0 comments on commit e496d2d

Please sign in to comment.