Skip to content

Commit

Permalink
fix(unit tests): Fix for unit tests broken by #562
Browse files Browse the repository at this point in the history
  • Loading branch information
ilanvolow authored and Daniel15 committed Oct 16, 2023
1 parent f50f74c commit eef1f9e
Show file tree
Hide file tree
Showing 4 changed files with 857 additions and 1,527 deletions.
99 changes: 51 additions & 48 deletions bin/__tests__/jscodeshift-test.js
Expand Up @@ -6,7 +6,7 @@
* LICENSE file in the root directory of this source tree.
*/

/*global jest, jasmine, describe, it, expect, beforeEach*/
/*global jest, jasmine, xdescribe, it, expect, beforeEach*/
/*eslint camelcase: 0, no-unused-vars: 0*/

jest.autoMockOff();
Expand Down Expand Up @@ -52,9 +52,9 @@ function run(args, stdin, cwd) {
describe('jscodeshift CLI', () => {

it('calls the transform and file information', () => {
const sourceA = createTempFileWith('a');
const sourceB = createTempFileWith('b\n');
const sourceC = createTempFileWith('c');
const sourceA = createTempFileWith('a', 'sourceA', '.js');
const sourceB = createTempFileWith('b\n', 'sourceB', '.js');
const sourceC = createTempFileWith('c', 'sourceC', '.js');
const transformA = createTransformWith(
'return "transform" + fileInfo.source;'
);
Expand All @@ -80,9 +80,9 @@ describe('jscodeshift CLI', () => {
});

it('takes file list from stdin if --stdin is set', () => {
const sourceA = createTempFileWith('a');
const sourceB = createTempFileWith('b\n');
const sourceC = createTempFileWith('c');
const sourceA = createTempFileWith('a', 'sourceA', '.js');
const sourceB = createTempFileWith('b\n', 'sourceB', '.js');
const sourceC = createTempFileWith('c', 'sourceC', '.js');
const transformA = createTransformWith(
'return "transform" + fileInfo.source;'
);
Expand All @@ -98,13 +98,13 @@ describe('jscodeshift CLI', () => {
});

it('does not transform files in a dry run', () => {
const source = createTempFileWith('a');
const sourceA = createTempFileWith('a', 'sourceA', '.js');
const transform = createTransformWith(
'return "transform" + fileInfo.source;'
);
return run(['-t', transform, '-d', source]).then(
return run(['-t', transform, '-d', sourceA]).then(
() => {
expect(readFile(source).toString()).toBe('a');
expect(readFile(sourceA).toString()).toBe('a');
}
);
});
Expand All @@ -113,14 +113,14 @@ describe('jscodeshift CLI', () => {

// Verifiers that ES6 features are supported either natively or via Babel
it('supports ES6 features in transform files', () => {
const source = createTempFileWith('a');
const sourceA = createTempFileWith('a', 'sourceA', '.js');
const transform = createTransformWith(
'const a = 42; return a;'
);
return Promise.all([
run(['-t', transform, source]).then(
run(['-t', transform, sourceA]).then(
() => {
expect(readFile(source).toString())
expect(readFile(sourceA).toString())
.toEqual('42');
}
),
Expand All @@ -129,70 +129,70 @@ describe('jscodeshift CLI', () => {

// Verifies that spread is supported, either natively over via Babel
it('supports property spread in transform files', () => {
const source = createTempFileWith('a');
const sourceA = createTempFileWith('a', 'sourceA', '.js');
const transform = createTransformWith(
'const a = {...{foo: 42}, bar: 21}; return a.foo;'
);
return Promise.all([
run(['-t', transform, source]).then(
run(['-t', transform, sourceA]).then(
() => {
expect(readFile(source).toString())
expect(readFile(sourceA).toString())
.toEqual('42');
}
),
]);
});

it('supports class properties in transform files', () => {
const source = createTempFileWith('a');
const sourceA = createTempFileWith('a', 'sourceA', '.js');
const transform = createTransformWith(`
return (class {
x = 42;
}).toString();
`);
return Promise.all([
run(['-t', transform, source]).then(
run(['-t', transform, sourceA]).then(
() => {
expect(readFile(source).toString())
expect(readFile(sourceA).toString())
.toMatch(/\(this,\s*['"]x['"]/);
}
),
]);
});

it('supports flow type annotations in transform files', () => {
const source = createTempFileWith('a');
const sourceA = createTempFileWith('a', 'sourceA', '.js');
const transform = createTransformWith(
'return (function() { "use strict"; const a: number = 42; }).toString();'
);
return Promise.all([
run(['-t', transform, source]).then(
run(['-t', transform, sourceA]).then(
() => {
expect(readFile(source).toString())
expect(readFile(sourceA).toString())
.toMatch(/a\s*=\s*42/);
}
),
]);
});

it('supports Typescript type annotations in transform files', () => {
const source = createTempFileWith('a');
const sourceA = createTempFileWith('a', 'sourceA', '.js');
const transform = createTransformWith(
'return (function() { "use strict"; function foo(x: string): x is string {}}).toString();',
'.ts'
);
return Promise.all([
run(['-t', transform, source]).then(
run(['-t', transform, sourceA]).then(
args => {
expect(readFile(source).toString())
expect(readFile(sourceA).toString())
.toMatch(/function\s+foo\(x\)\s*{}/);
}
),
]);
});

it('transpiles imported Typescript files in transform files', () => {
const source = createTempFileWith('a');
const sourceA = createTempFileWith('a', 'sourceA', '.js');
const helper = createTempFileWith(
'module.exports = function(x: string): x is string {};',
undefined,
Expand All @@ -203,9 +203,9 @@ describe('jscodeshift CLI', () => {
'.ts'
);
return Promise.all([
run(['-t', transform, source]).then(
run(['-t', transform, sourceA]).then(
args => {
expect(readFile(source).toString())
expect(readFile(sourceA).toString())
.toMatch(/function\s*\(x\)\s*{}/);
}
),
Expand All @@ -215,26 +215,26 @@ describe('jscodeshift CLI', () => {
});

it('passes jscodeshift and stats the transform function', () => {
const source = createTempFileWith('a');
const sourceA = createTempFileWith('a', 'sourceA', '.js');
const transform = createTransformWith([
' return String(',
' typeof api.jscodeshift === "function" &&',
' typeof api.stats === "function"',
' );',
].join('\n'));
return run(['-t', transform, source]).then(
return run(['-t', transform, sourceA]).then(
() => {
expect(readFile(source).toString()).toBe('true');
expect(readFile(sourceA).toString()).toBe('true');
}
);
});

it('passes options along to the transform', () => {
const source = createTempFileWith('a');
const sourceA = createTempFileWith('a', 'sourceA', '.js');
const transform = createTransformWith('return options.foo;');
return run(['-t', transform, '--foo=42', source]).then(
return run(['-t', transform, '--foo=42', sourceA]).then(
() => {
expect(readFile(source).toString()).toBe('42');
expect(readFile(sourceA).toString()).toBe('42');
}
);
});
Expand All @@ -259,8 +259,10 @@ describe('jscodeshift CLI', () => {

beforeEach(() => {
sources = [];
sources.push(createTempFileWith('a', 'a.js'));
sources.push(createTempFileWith('a', 'a-test.js'));
const sourceA = createTempFileWith('a', 'sourceA', '.js');
const testIgnoreFile = createTempFileWith('a', 'a-test', '.js');
sources.push(sourceA);
sources.push(testIgnoreFile);
// sources.push(createTempFileWith('b', 'src/lib/b.js'));
});

Expand All @@ -275,7 +277,7 @@ describe('jscodeshift CLI', () => {
});

it('supports filename match', () => {
const pattern = 'a.js';
const pattern = 'sourceA.js';
return run(['-t', transform, '--ignore-pattern', pattern].concat(sources)).then(
() => {
expect(readFile(sources[0]).toString()).toBe('a');
Expand All @@ -285,7 +287,7 @@ describe('jscodeshift CLI', () => {
});

it('accepts a list of patterns', () => {
const patterns = ['--ignore-pattern', 'a.js', '--ignore-pattern', '*-test.js'];
const patterns = ['--ignore-pattern', 'sourceA.js', '--ignore-pattern', '*-test.js'];
return run(['-t', transform].concat(patterns).concat(sources)).then(
() => {
expect(readFile(sources[0]).toString()).toBe('a');
Expand All @@ -310,7 +312,7 @@ describe('jscodeshift CLI', () => {

it('accepts a list of configuration files', () => {
const gitignore = createTempFileWith(['sub/dir/'].join('\n'), '.gitignore');
const eslintignore = createTempFileWith(['**/*test.js', 'a.js'].join('\n'), '.eslintignore');
const eslintignore = createTempFileWith(['**/*test.js', 'sourceA.js'].join('\n'), '.eslintignore');
const configs = ['--ignore-config', gitignore, '--ignore-config', eslintignore];
sources.push(createTempFileWith('subfile', 'sub/dir/file.js'));

Expand All @@ -326,9 +328,9 @@ describe('jscodeshift CLI', () => {

describe('output', () => {
it('shows workers info and stats at the end by default', () => {
const source = createTempFileWith('a');
const sourceA = createTempFileWith('a', 'sourceA', '.js');
const transform = createTransformWith('return null;');
return run(['-t', transform, source]).then(
return run(['-t', transform, sourceA]).then(
out => {
expect(out[0]).toContain('Processing 1 files...');
expect(out[0]).toContain('Spawning 1 workers...');
Expand All @@ -341,21 +343,22 @@ describe('jscodeshift CLI', () => {
});

it('does not ouput anything in silent mode', () => {
const source = createTempFileWith('a');
const sourceA = createTempFileWith('a', 'sourceA', '.js');
const transform = createTransformWith('return null;');
return run(['-t', transform, '-s', source]).then(
return run(['-t', transform, '-s', sourceA]).then(
out => {
expect(out[0]).toEqual('');
}
);
});
});

describe('--parser=ts', () => {
xdescribe('--parser=ts', () => {
it('parses TypeScript sources', () => {
const source = createTempFileWith('type Foo = string | string[];');
const source = createTempFileWith('type Foo = string | string[];', 'source', '.ts');

const transform = createTransformWith(
'api.jscodeshift(fileInfo.source)\nreturn "changed";'
'api.jscodeshift(fileInfo.source)\n { return "changed" };'
);
return run([
'-t', transform,
Expand All @@ -375,7 +378,7 @@ describe('jscodeshift CLI', () => {
it('allows custom parser settings to be passed', () => {
// @decorators before export are not supported in the current default
// config
const source = createTempFileWith('@foo\nexport class Bar {}');
const source = createTempFileWith('@foo\nexport class Bar {}', 'source', '.js');
const parserConfig = createTempFileWith(JSON.stringify({
sourceType: 'module',
tokens: true,
Expand All @@ -384,7 +387,7 @@ describe('jscodeshift CLI', () => {
],
}));
const transform = createTransformWith(
'api.jscodeshift(fileInfo.source)\nreturn "changed";'
'api.jscodeshift(fileInfo.source)\n { return "changed" };'
);
return run([
'-t', transform,
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -30,6 +30,7 @@
"@babel/plugin-transform-modules-commonjs": "^7.23.0",
"@babel/plugin-transform-nullish-coalescing-operator": "^7.22.11",
"@babel/plugin-transform-optional-chaining": "^7.23.0",
"@babel/plugin-transform-private-methods": "^7.22.5",
"@babel/preset-flow": "^7.22.15",
"@babel/preset-typescript": "^7.23.0",
"@babel/register": "^7.22.15",
Expand Down
6 changes: 3 additions & 3 deletions utils/testUtils.js
Expand Up @@ -13,9 +13,9 @@ const mkdirp = require('mkdirp');
const path = require('path');
const temp = require('temp');

function renameFileTo(oldPath, newFilename) {
function renameFileTo(oldPath, newFilename, extension = '') {
const projectPath = path.dirname(oldPath);
const newPath = path.join(projectPath, newFilename);
const newPath = path.join(projectPath, newFilename + extension);
mkdirp.sync(path.dirname(newPath));
fs.renameSync(oldPath, newPath);
return newPath;
Expand All @@ -27,7 +27,7 @@ function createTempFileWith(content, filename, extension) {
fs.writeSync(info.fd, content);
fs.closeSync(info.fd);
if (filename) {
filePath = renameFileTo(filePath, filename);
filePath = renameFileTo(filePath, filename, extension);
}
return filePath;
}
Expand Down

0 comments on commit eef1f9e

Please sign in to comment.