Skip to content

Commit

Permalink
Moved --watch-ignored test into parent test
Browse files Browse the repository at this point in the history
  • Loading branch information
ankushduacodes committed Dec 18, 2020
1 parent 9991bb4 commit 18c199c
Showing 1 changed file with 46 additions and 47 deletions.
93 changes: 46 additions & 47 deletions tests/unit/test.watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,58 +207,57 @@ describe('watcher', () => {

});

});

describe('--watch-ignored is passed in', () => {
it('does not change if ignored file is touched', () =>
withTempDir(async (tmpDir) => {
const debounceTime = 10;
const onChange = sinon.spy();
const tmpPath = tmpDir.path();
const files = ['foo.txt', 'bar.txt', 'foobar.txt'].map(
(filePath) => path.join(tmpPath, filePath)
);

const watcher = onSourceChange({
sourceDir: tmpPath,
artifactsDir: path.join(tmpPath, 'web-ext-artifacts'),
onChange,
watchIgnored: ['foo.txt'].map(
describe('--watch-ignored is passed in', () => {
it('does not change if ignored file is touched', () =>
withTempDir(async (tmpDir) => {
const debounceTime = 10;
const onChange = sinon.spy();
const tmpPath = tmpDir.path();
const files = ['foo.txt', 'bar.txt', 'foobar.txt'].map(
(filePath) => path.join(tmpPath, filePath)
),
shouldWatchFile: () => true,
debounceTime,
});
);

const watchAll = new Watchpack();
watchAll.watch(files, [], 0);
const watcher = onSourceChange({
sourceDir: tmpPath,
artifactsDir: path.join(tmpPath, 'web-ext-artifacts'),
onChange,
watchIgnored: ['foo.txt'].map(
(filePath) => path.join(tmpPath, filePath)
),
shouldWatchFile: () => true,
debounceTime,
});

async function waitDebounce() {
await new Promise((resolve) => setTimeout(resolve, debounceTime * 2));
}
const watchAll = new Watchpack();
watchAll.watch(files, [], 0);

async function assertOnChange(filePath, expectedCallCount) {
const promiseOnChanged = new Promise((resolve) => watchAll.once(
'change',
(f) => resolve(f))
);
await waitDebounce();
await fs.writeFile(filePath, '<content>');
assert.equal(filePath, await promiseOnChanged);
sinon.assert.callCount(onChange, expectedCallCount);
}
async function waitDebounce() {
await new Promise((resolve) => setTimeout(resolve, debounceTime * 2));
}

// Verify foo.txt is being ignored.
await assertOnChange(files[0], 0);
async function assertOnChange(filePath, expectedCallCount) {
const promiseOnChanged = new Promise((resolve) => watchAll.once(
'change',
(f) => resolve(f))
);
await waitDebounce();
await fs.writeFile(filePath, '<content>');
assert.equal(filePath, await promiseOnChanged);
sinon.assert.callCount(onChange, expectedCallCount);
}

// Verify that the other two files are not be ignored.
await assertOnChange(files[1], 1);
await assertOnChange(files[2], 2);
// Verify foo.txt is being ignored.
await assertOnChange(files[0], 0);

watcher.close();
watchAll.close();
// Leave watcher.close some time to complete its cleanup before withTempDir will remove the
// test directory.
await waitDebounce();
}));
// Verify that the other two files are not be ignored.
await assertOnChange(files[1], 1);
await assertOnChange(files[2], 2);

watcher.close();
watchAll.close();
// Leave watcher.close some time to complete its cleanup before withTempDir will remove the
// test directory.
await waitDebounce();
}));
});
});

0 comments on commit 18c199c

Please sign in to comment.