Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests(utils): move tests to TS #688

Merged
merged 21 commits into from
Nov 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0a590a0
chore(test): add migrate test
sendilkumarn Sep 25, 2018
dc79068
chore(lint): revert change
sendilkumarn Sep 25, 2018
dd3a76c
chore(test): moved to typescript migrate tests
ematipico Sep 29, 2018
aa80327
tests(ts): moved module concatenation plugin to ts
ematipico Sep 29, 2018
5a34a88
tests(migrate): moved to typescript
ematipico Oct 1, 2018
122a770
tests(webpack-scaffold): moved to typescript
hemal7735 Nov 6, 2018
3a9656a
tests(utils): copy fixtures to __tests_
hemal7735 Nov 7, 2018
a3c9e32
tests(utils): move package-manager.test.js to TS
hemal7735 Nov 7, 2018
bb7e3ed
tests(utils): move validate-identifier.test.js to TS
hemal7735 Nov 7, 2018
655715d
tests(utils): move resolve-packages.test.js to TS
hemal7735 Nov 7, 2018
f470f5f
tests(utils): is-local-path.test.js to TS
hemal7735 Nov 7, 2018
182f0bb
tests(utils): move npm-exists.test.js to TS
hemal7735 Nov 7, 2018
86b5d7d
tests(utils): move ast-utils.test.js to TS
hemal7735 Nov 7, 2018
c2c1479
tests(utils): WIP-test-cases
hemal7735 Nov 8, 2018
b351fa2
tests(utils): move recursive-parser.test.js to TS
hemal7735 Nov 12, 2018
7bda1ff
tests(utils): move npm-package-exists.test.js to TS
hemal7735 Nov 12, 2018
1c8d42a
tests(utils): cleanup .js , __fixtures__ and __snapshots__ files
hemal7735 Nov 12, 2018
cbb9bae
chore(utils): clean console recursive-parser.test.ts
hemal7735 Nov 16, 2018
46365a2
chore(utils): make options param optional
hemal7735 Nov 16, 2018
aec65b8
Merge remote-tracking branch 'GRO/ts-test' into ts-test-utils
hemal7735 Nov 16, 2018
8af2a8b
chore: use INode in ast-utils.test.ts
hemal7735 Nov 17, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
"testRegex": "/__tests__/.*\\.(test.js|test.ts)$",
"moduleFileExtensions": [
"ts",
"js"
"js",
"json"
]
},
"nyc": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,27 @@ exports[`utils addProperty add entry property using add 1`] = `
}],

man: () => duper,
man: () => duper,
nice: ':)',
objects: are,

super: [yeah, {
loader: 'eslint-loader'
}],

nice: ':)',
man: () => duper
}]
}
}"
`;

exports[`utils addProperty add entry property using init 1`] = `
"module.exports = {
entry: {
man: () => duper,
nice: ':)',
objects: are,

super: [yeah, {
loader: 'eslint-loader'
}],

nice: ':)',
man: () => duper
}]
}
}"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ module.exports = {
foo: \\"Promise.resolve()\\",
man: \\"() => duper\\",
mode: \\"yaaa\\",
foo: Promise.resolve(),
man: () => nice!!,
mode: super-man,
nice: '=)',
objects: are not,

super: [op, {
test: /\\\\.(wasm|c)$/,
loader: 'pia-loader',
enforce: 'pre',
include: [asd, 'Stringy'],
loader: 'pia-loader',

options: {
formatter: 'nao'
}
}],
},

nice: '=)',
foo: Promise.resolve(),
man: () => nice!!,
mode: super-man
test: /\\\\.(wasm|c)$/
}]
}
}
"
Expand All @@ -50,22 +50,22 @@ module.exports = {
exports[`init transforms correctly using "fixture-1" data 1`] = `
"module.exports = {
entry: {
foo: Promise.resolve(),
man: () => duper,
nice: ':)',
objects: are,

super: [yeah, {
test: /\\\\.(js|vue)$/,
loader: 'eslint-loader',
enforce: 'pre',
include: [customObj, 'Stringy'],
loader: 'eslint-loader',

options: {
formatter: 'someOption'
}
}],
},

nice: ':)',
foo: Promise.resolve(),
man: () => duper
test: /\\\\.(js|vue)$/
}]
}
};"
`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

const j = require("jscodeshift/dist/core");
const utils = require("./ast-utils");
import * as j from "jscodeshift/dist/core";
import * as utils from "../ast-utils";
import { INode } from "../types/NodePath";

describe("utils", () => {
describe("createProperty", () => {
Expand Down Expand Up @@ -105,41 +106,41 @@ const a = { plugs: [] }
describe("createOrUpdatePluginByName", () => {
it("should create a new plugin without arguments", () => {
const ast = j("{ plugins: [] }");
ast.find(j.ArrayExpression).forEach(node => {
ast.find(j.ArrayExpression).forEach((node: INode) => {
utils.createOrUpdatePluginByName(j, node, "Plugin");
});
expect(ast.toSource()).toMatchSnapshot();
});

it("should create a new plugin with arguments", () => {
const ast = j("{ plugins: [] }");
ast.find(j.ArrayExpression).forEach(node => {
ast.find(j.ArrayExpression).forEach((node: INode) => {
utils.createOrUpdatePluginByName(j, node, "Plugin", {
foo: "bar"
foo: "bar",
});
});
expect(ast.toSource()).toMatchSnapshot();
});

it("should add an object as an argument", () => {
const ast = j("[new Plugin()]");
ast.find(j.ArrayExpression).forEach(node => {
ast.find(j.ArrayExpression).forEach((node: INode) => {
utils.createOrUpdatePluginByName(j, node, "Plugin", {
foo: true
foo: true,
});
});
expect(ast.toSource()).toMatchSnapshot();
});

it("should merge options objects", () => {
const ast = j("[new Plugin({ foo: true })]");
ast.find(j.ArrayExpression).forEach(node => {
ast.find(j.ArrayExpression).forEach((node: INode) => {
utils.createOrUpdatePluginByName(j, node, "Plugin", {
bar: "baz",
foo: false
foo: false,
});
utils.createOrUpdatePluginByName(j, node, "Plugin", {
"baz-long": true
"baz-long": true,
});
});
expect(ast.toSource()).toMatchSnapshot();
Expand Down Expand Up @@ -191,8 +192,8 @@ const a = { plugs: [] }
expect(
ast
.find(j.ObjectExpression)
.filter(p => utils.findObjWithOneOfKeys(p, ["a"]))
.size()
.filter((p) => utils.findObjWithOneOfKeys(p, ["a"]))
.size(),
).toEqual(1);
});
});
Expand All @@ -207,27 +208,27 @@ const a = { plugs: [] }
describe("safeTraverse", () => {
it("should safe traverse", () => {
const testObject = {
type: "NodeType"
type: "NodeType",
};
const p = {
foo: {
bar: testObject
}
bar: testObject,
},
};
const require = utils.safeTraverse(p, ["foo", "bar"]);
expect(require).toEqual(testObject);
});

it("should safe traverse thrice", () => {
const type = {
type: "NodeType"
type: "NodeType",
};
const p = {
parent: {
value: {
value: type
}
}
value: type,
},
},
};
const traversedValue = utils.safeTraverse(p, ["parent", "value", "value"]);
expect(traversedValue).toEqual(type);
Expand All @@ -240,9 +241,9 @@ const a = { plugs: [] }
const p = {
value: {
value: {
type: NODE_TYPE
}
}
type: NODE_TYPE,
},
},
};
const typeValue = utils.safeTraverseAndGetType(p);
expect(typeValue).toEqual(NODE_TYPE);
Expand All @@ -253,9 +254,9 @@ const a = { plugs: [] }
const p = {
foo: {
bar: {
type: NODE_TYPE
}
}
type: NODE_TYPE,
},
},
};
const typeValue = utils.safeTraverseAndGetType(p);
expect(typeValue).toEqual(false);
Expand All @@ -266,20 +267,20 @@ const a = { plugs: [] }
it("add entry property using init", () => {
const ast = j("module.exports = {}");
const propertyValue = {
man: "() => duper",
nice: "':)'",
objects: "are",
super: [
"yeah",
{
loader: "'eslint-loader'"
}
loader: "'eslint-loader'",
},
],
nice: "':)'",
man: "() => duper"
};

const root = ast.find(j.ObjectExpression);

root.forEach(p => {
root.forEach((p) => {
utils.addProperty(j, p, "entry", propertyValue);
});

Expand All @@ -299,20 +300,20 @@ const a = { plugs: [] }
}
}`);
const propertyValue = {
man: "() => duper",
nice: "':)'",
objects: "are",
super: [
"yeah",
{
loader: "'eslint-loader'"
}
loader: "'eslint-loader'",
},
],
nice: "':)'",
man: "() => duper"
};

const root = ast.find(j.ObjectExpression);

utils.findRootNodesByName(j, root, "entry").forEach(p => {
utils.findRootNodesByName(j, root, "entry").forEach((p) => {
j(p).replaceWith(utils.addProperty(j, p, "entry", propertyValue, "add"));
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const isLocalPath = require("./is-local-path").default;
const path = require("path");
import * as path from "path";
import isLocalPath from "../is-local-path";

describe("is-local-path", () => {
it("returns true for paths beginning in the current directory", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use strict";
const exists = require("./npm-exists").default;
import exists from "../npm-exists";

describe("npm-exists", () => {
it("should successfully existence of a published module", () => {
exists("webpack-addons-ylvis").then(status => {
exists("webpack-addons-ylvis").then((status) => {
expect(status).toBe(true);
});
});

it("should return false for the existence of a fake module", () => {
exists("webpack-scaffold-noop").then(status => {
exists("webpack-scaffold-noop").then((status) => {
expect(status).toBe(false);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const npmPackagesExists = require("./npm-packages-exists").default;
// console.log('npmPackagesExists: ', npmPackagesExists);
import npmPackagesExists from "../npm-packages-exists";
import {resolvePackages} from "../resolve-packages";

jest.mock("./npm-exists");
jest.mock("./resolve-packages");
jest.mock("../npm-exists");
jest.mock("../resolve-packages");

const mockResolvePackages = require("./resolve-packages").resolvePackages;
// TS is not aware that jest changes the type of resolvePackages
const mockResolvePackages = resolvePackages as jest.Mock<typeof resolvePackages>;

describe("npmPackagesExists", () => {
test("resolves packages when they are available on the local filesystem", () => {
Expand All @@ -16,12 +17,12 @@ describe("npmPackagesExists", () => {
expect(() => npmPackagesExists(["my-webpack-addon"])).toThrowError(TypeError);
});

test("resolves packages when they are available on npm", done => {
require("./npm-exists").default.mockImplementation(() => Promise.resolve(true));
test("resolves packages when they are available on npm", (done) => {
require("../npm-exists").default.mockImplementation(() => Promise.resolve(true));
npmPackagesExists(["webpack-scaffold-foobar"]);
setTimeout(() => {
expect(mockResolvePackages.mock.calls[mockResolvePackages.mock.calls.length - 1][0]).toEqual([
"webpack-scaffold-foobar"
"webpack-scaffold-foobar",
]);
done();
}, 10);
Expand Down