Skip to content

Commit

Permalink
feat(jest-haste-map): Enable crawling for symlink test files
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmeku committed Jan 1, 2020
1 parent 9419034 commit 625d668
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- `[jest-environment-node]` Implement `compileFunction` ([#9140](https://github.com/facebook/jest/pull/9140))
- `[@jest/fake-timers]` Add Lolex as implementation of fake timers ([#8897](https://github.com/facebook/jest/pull/8897))
- `[jest-get-type]` Add `BigInt` support. ([#8382](https://github.com/facebook/jest/pull/8382))
- `[jest-haste-map]` Enable crawling for symlinked test files ([#9350](https://github.com/facebook/jest/issues/9350))
- `[jest-matcher-utils]` Add `BigInt` support to `ensureNumbers` `ensureActualIsNumber`, `ensureExpectedIsNumber` ([#8382](https://github.com/facebook/jest/pull/8382))
- `[jest-reporters]` Export utils for path formatting ([#9162](https://github.com/facebook/jest/pull/9162))
- `[jest-runner]` Warn if a worker had to be force exited ([#8206](https://github.com/facebook/jest/pull/8206))
Expand Down
19 changes: 19 additions & 0 deletions e2e/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ export const writeFiles = (
});
};

export const writeSymlinks = (
directory: string,
symlinks: {[existingFile: string]: string},
) => {
createDirectory(directory);
Object.keys(symlinks).forEach(fileOrPath => {
const symLinkPath = symlinks[fileOrPath];
const dirname = path.dirname(symLinkPath);

if (dirname !== '/') {
createDirectory(path.join(directory, dirname));
}
fs.symlinkSync(
path.resolve(directory, ...fileOrPath.split('/')),
path.resolve(directory, ...symLinkPath.split('/')),
);
});
};

const NUMBER_OF_TESTS_TO_FORCE_USING_WORKERS = 25;
/**
* Forces Jest to use workers by generating many test files to run.
Expand Down
34 changes: 34 additions & 0 deletions e2e/__tests__/crawlSymlinks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import runJest from '../runJest';
import {tmpdir} from 'os';
import * as path from 'path';
import {writeFiles, writeSymlinks} from '../Utils';
import * as fs from 'fs';
const DIR = path.resolve(tmpdir(), 'crawl-symlinks-test');

test('Node crawler picks up symlinked files', () => {
fs.rmdirSync(DIR, {recursive: true});

writeFiles(DIR, {
'package.json': `
{
"jest": {
"testMatch": ["<rootDir>/test-files/test.js"]
}
}
`,
'symlinked-files/test.js': `
test('1+1', () => {
expect(1).toBe(1);
});
`,
});

writeSymlinks(DIR, {
'symlinked-files/test.js': 'test-files/test.js',
});

const {stdout, stderr, exitCode} = runJest(DIR, ['--no-watchman']);
expect(stderr).toContain('Test Suites: 1 passed, 1 total');
expect(stdout).toEqual('');
expect(exitCode).toEqual(0);
});
5 changes: 5 additions & 0 deletions packages/jest-haste-map/src/crawlers/__tests__/node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ describe('node crawler', () => {
expect(childProcess.spawn).lastCalledWith('find', [
'/project/fruits',
'/project/vegtables',
'(',
'-type',
'f',
'-o',
'-type',
'l',
')',
'(',
'-iname',
'*.js',
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-haste-map/src/crawlers/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ function findNative(
callback: Callback,
): void {
const args = Array.from(roots);
args.push('-type', 'f');
args.push('(', '-type', 'f', '-o', '-type', 'l', ')');

if (extensions.length) {
args.push('(');
}
Expand Down Expand Up @@ -121,7 +122,8 @@ function findNative(
} else {
lines.forEach(path => {
fs.stat(path, (err, stat) => {
if (!err && stat) {
// Filter out symlinks that describe directories
if (!err && stat && !stat.isDirectory()) {
result.push([path, stat.mtime.getTime(), stat.size]);
}
if (--count === 0) {
Expand Down

0 comments on commit 625d668

Please sign in to comment.