Skip to content

Commit

Permalink
Refactored promises to avoid possible fs stream concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
Connormiha committed Jul 12, 2019
1 parent a560aa0 commit 1d93984
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions packages/jest-core/src/__tests__/watch-file-changes.test.ts
Expand Up @@ -35,6 +35,8 @@ describe('Watch mode flows with changed files', () => {
watch = require('../watch').default;
pipe = {write: jest.fn()} as any;
stdin = new MockStdin();
rimraf.sync(cacheDirectory);
rimraf.sync(testDirectory);
fs.mkdirSync(testDirectory);
fs.mkdirSync(cacheDirectory);
});
Expand All @@ -44,11 +46,6 @@ describe('Watch mode flows with changed files', () => {
if (hasteMapInstance) {
hasteMapInstance.end();
}
[fileTargetPath2, fileTargetPath].forEach(file => {
try {
fs.unlinkSync(file);
} catch (e) {}
});
rimraf.sync(cacheDirectory);
rimraf.sync(testDirectory);
});
Expand Down Expand Up @@ -109,6 +106,9 @@ describe('Watch mode flows with changed files', () => {
);

const hook = new JestHook();
const firstErrorPromise = new Promise(resolve => {
hook.getSubscriber().onTestRunComplete(resolve);
});
await watch(
{
...config,
Expand All @@ -121,7 +121,9 @@ describe('Watch mode flows with changed files', () => {
hook,
);

await new Promise(resolve => {
await firstErrorPromise;

const successPromise: Promise<AggregatedResult> = new Promise(resolve => {
hook.getSubscriber().onTestRunComplete(resolve);
});

Expand All @@ -138,9 +140,7 @@ describe('Watch mode flows with changed files', () => {
{encoding: 'utf-8'},
);

const resultSuccessReport: AggregatedResult = await new Promise(resolve => {
hook.getSubscriber().onTestRunComplete(resolve);
});
const resultSuccessReport: AggregatedResult = await successPromise;

expect(resultSuccessReport).toMatchObject({
numFailedTestSuites: 0,
Expand All @@ -154,12 +154,14 @@ describe('Watch mode flows with changed files', () => {
failureMessage: null,
});

const errorPromise: Promise<AggregatedResult> = new Promise(resolve => {
hook.getSubscriber().onTestRunComplete(resolve);
});

// Remove again to ensure about no legacy cache
fs.unlinkSync(fileTargetPath);

const resultErrorReport: AggregatedResult = await new Promise(resolve => {
hook.getSubscriber().onTestRunComplete(resolve);
});
const resultErrorReport: AggregatedResult = await errorPromise;

// After remove file we have to fail tests
expect(resultErrorReport).toMatchObject({
Expand Down

0 comments on commit 1d93984

Please sign in to comment.