Skip to content

Commit

Permalink
test(detectJestVersion): strip ansi before comparing (#1450)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Oct 20, 2023
1 parent 837be60 commit 82ab20a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
"rimraf": "^5.0.0",
"semantic-release": "^22.0.0",
"semver": "^7.3.5",
"strip-ansi": "^6.0.0",
"ts-node": "^10.2.1",
"typescript": "^5.0.4"
},
Expand Down
34 changes: 19 additions & 15 deletions src/rules/utils/__tests__/detectJestVersion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import type { JSONSchemaForNPMPackageJsonFiles } from '@schemastore/package';
import stripAnsi from 'strip-ansi';
import { create } from 'ts-node';
import { detectJestVersion } from '../detectJestVersion';

Expand All @@ -18,10 +19,13 @@ const compiledFn = compileFnCode(require.resolve('../detectJestVersion.ts'));
const relativePathToFn = 'eslint-plugin-jest/lib/rules/detectJestVersion.js';

const runNodeScript = (cwd: string, script: string) => {
return spawnSync('node', ['-e', script.split('\n').join(' ')], {
cwd,
encoding: 'utf-8',
});
const { stdout, stderr } = spawnSync(
'node',
['-e', script.split('\n').join(' ')],
{ cwd, encoding: 'utf-8' },
);

return { stdout: stripAnsi(stdout.trim()), stderr: stripAnsi(stderr.trim()) };
};

const runDetectJestVersion = (cwd: string) => {
Expand Down Expand Up @@ -120,8 +124,8 @@ describe('detectJestVersion', () => {

const { stdout, stderr } = runDetectJestVersion(projectDir);

expect(stdout.trim()).toBe('21');
expect(stderr.trim()).toBe('');
expect(stdout).toBe('21');
expect(stderr).toBe('');
});
});

Expand All @@ -140,8 +144,8 @@ describe('detectJestVersion', () => {

const { stdout, stderr } = runDetectJestVersion(projectDir);

expect(stdout.trim()).toBe('19');
expect(stderr.trim()).toBe('');
expect(stdout).toBe('19');
expect(stderr).toBe('');
});
});

Expand All @@ -165,14 +169,14 @@ describe('detectJestVersion', () => {
const { stdout: stdoutBackend, stderr: stderrBackend } =
runDetectJestVersion(path.join(projectDir, 'backend'));

expect(stdoutBackend.trim()).toBe('24');
expect(stderrBackend.trim()).toBe('');
expect(stdoutBackend).toBe('24');
expect(stderrBackend).toBe('');

const { stdout: stdoutFrontend, stderr: stderrFrontend } =
runDetectJestVersion(path.join(projectDir, 'frontend'));

expect(stdoutFrontend.trim()).toBe('15');
expect(stderrFrontend.trim()).toBe('');
expect(stdoutFrontend).toBe('15');
expect(stderrFrontend).toBe('');
});
});

Expand All @@ -186,8 +190,8 @@ describe('detectJestVersion', () => {

const { stdout, stderr } = runDetectJestVersion(projectDir);

expect(stdout.trim()).toBe('');
expect(stderr.trim()).toContain('Unable to detect Jest version');
expect(stdout).toBe('');
expect(stderr).toContain('Unable to detect Jest version');
});
});

Expand Down Expand Up @@ -221,7 +225,7 @@ describe('detectJestVersion', () => {

expect(firstCall).toBe('26');
expect(secondCall).toBe('26');
expect(stderr.trim()).toBe('');
expect(stderr).toBe('');
});
});
});
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5096,6 +5096,7 @@ __metadata:
rimraf: ^5.0.0
semantic-release: ^22.0.0
semver: ^7.3.5
strip-ansi: ^6.0.0
ts-node: ^10.2.1
typescript: ^5.0.4
peerDependencies:
Expand Down

0 comments on commit 82ab20a

Please sign in to comment.