Skip to content

Commit

Permalink
tests(utils): move tests to TS (#688)
Browse files Browse the repository at this point in the history
* chore(test): add migrate test

* chore(lint): revert change

* chore(test): moved to typescript migrate tests

* tests(ts): moved module concatenation plugin to ts

* tests(migrate): moved to typescript

* tests(webpack-scaffold): moved to typescript

* tests(utils): copy fixtures to __tests_

* tests(utils): move package-manager.test.js to TS

* tests(utils): move validate-identifier.test.js to TS

* tests(utils): move resolve-packages.test.js to TS

* tests(utils): is-local-path.test.js to TS

* tests(utils): move npm-exists.test.js to TS

* tests(utils): move ast-utils.test.js to TS

* tests(utils): WIP-test-cases

* tests(utils): move recursive-parser.test.js to TS

* tests(utils): move npm-package-exists.test.js to TS

* tests(utils): cleanup .js , __fixtures__ and __snapshots__ files

* chore(utils): clean console recursive-parser.test.ts

* chore(utils): make options param optional

* chore: use INode in ast-utils.test.ts
  • Loading branch information
hemal7735 authored and ematipico committed Nov 17, 2018
1 parent 4c14ccf commit 243ea58
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 167 deletions.
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

0 comments on commit 243ea58

Please sign in to comment.