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

feat(tests): adds basic testing support #8

Merged
merged 8 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ version: 2
jobs:
build:
docker:
- image: 'circleci/node:latest'
- image: "circleci/node:latest"
steps:
- checkout
- run:
name: install
command: npm install
- run:
name: test
command: npm test
- run:
name: release
command: npm run semantic-release || true
5 changes: 5 additions & 0 deletions __mocks__/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const fs = jest.genMockFromModule<any>("fs");

fs.existsSync = () => true;

module.exports = fs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your thoughts on mocking the function instead of mocking the whole module?

const mock = jest.spyOn(fs, 'readFileSync');  // spy on fs.readFileSync()
    mock.mockImplementation(() => JSON.stringify({ name: 'myname' }));

Would that a better idea? And would it solve the type issue we're currently trying to solve?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not.

Screen Shot 2019-10-31 at 7 29 10 AM

Since the instance of the import in the test is different in the actual app code, the mock doesn't apply to the right one AFAIK.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, gotcha. Well, I don't think we should hold this back for that reason.

Ping me when you think this is ready to go and I'll approve!

11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
"@semantic-release/git": "^7.0.17",
"@semantic-release/npm": "^5.3.2",
"@types/node": "^12.12.3",
"@types/jest": "^24.0.21",
"globby": "^10",
"jest": "^24.9.0",
"semantic-release": "^15.13.28",
"ts-jest": "^24.1.0",
"ts-node": "^8",
"typescript": "^3.3"
"typescript": "^3.6.4"
},
"engines": {
"node": ">=8.0.0"
Expand Down Expand Up @@ -50,7 +53,7 @@
"scripts": {
"postpack": "rm -f oclif.manifest.json",
"prepack": "rm -rf lib && tsc -b && oclif-dev manifest && oclif-dev readme",
"test": "echo NO TESTS",
"test": "jest",
"version": "oclif-dev readme && git add README.md",
"gen": "hygen",
"semantic-release": "semantic-release"
Expand All @@ -69,5 +72,9 @@
}
]
]
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node"
}
}
32 changes: 8 additions & 24 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import { Command, flags } from "@oclif/command";
import { execSync } from "child_process";
import * as path from "path";
import * as fs from "fs";

// The root of our package, so that we can leverage the scripts in the `package.json`
const rootDirectory = path.join(__dirname, "..");
const defaultTemplatePath = `${rootDirectory}/_templates`;
// Grab the path of the user's project
const pathWhereScriptIsRunning = process.cwd();

// This is where their templates are (at least we assume so)
const theirTemplatePath = pathWhereScriptIsRunning + "/_templates";

// Check if they have a template directory
const hasTemplates = fs.existsSync(theirTemplatePath);
import {
getTemplateLocation,
pathWhereScriptIsRunning,
rootDirectory
} from "../utils/getTemplateLocation";

const DEFAULT_COMPONENT_NAME = "MyNewComponent";
const DEFAULT_TEMPLATE_NAME = "react-component";
Expand Down Expand Up @@ -56,23 +47,16 @@ export default class Generate extends Command {
const { flags } = this.parse(Generate);
const template = flags.template || DEFAULT_TEMPLATE_NAME;
const name = flags.name || DEFAULT_COMPONENT_NAME;
let templateLocation = "";
const templateLocation = getTemplateLocation();
const templatePath = `HYGEN_TMPLS=${templateLocation}`;

this.log(
`Generating new component using template ${template} at ./src/components/${name}.js`
);

// If they do have templates, use theirs
if (hasTemplates) {
templateLocation = `HYGEN_TMPLS=${theirTemplatePath}`;
} else {
// Otherwise, use ours
templateLocation = `HYGEN_TMPLS=${defaultTemplatePath}`;
}

// Generate template
execSync(
`${templateLocation} yarn gen ${template} new ${name} --path=${pathWhereScriptIsRunning}`,
`${templatePath} yarn gen ${template} new ${name} --path=${pathWhereScriptIsRunning}`,
{ cwd: rootDirectory, stdio: "inherit" }
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {run} from '@oclif/command'
export { run } from "@oclif/command";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have prettier set for this? If not we should (typically spaces before curlies and single quotes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, this is a global prettier config I'm using - we don't have one on the project yet.

27 changes: 27 additions & 0 deletions src/utils/getTemplateLocation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
afterEach(() => {
jest.resetModules();
});

test("uses default template if dev does not have custom ones", () => {
jest.dontMock("fs");

const {
DEFAULT_TEMPLATE_PATH,
getTemplateLocation
} = require("./getTemplateLocation");

const templateLocation = getTemplateLocation();

expect(templateLocation).toEqual(DEFAULT_TEMPLATE_PATH);
});

test("uses developer custom templates if defined", () => {
jest.mock("fs");
jest.spyOn(process, "cwd").mockImplementation(() => "/custom/path/to");

const { getTemplateLocation } = require("./getTemplateLocation");

const templateLocation = getTemplateLocation();

expect(templateLocation).toEqual("/custom/path/to/_templates");
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎸

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great test 👏

18 changes: 18 additions & 0 deletions src/utils/getTemplateLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as fs from "fs";
import * as path from "path";

// The root of our package, so that we can leverage the scripts in the `package.json`
export const rootDirectory = path.join(__dirname, "..");

export const DEFAULT_TEMPLATE_PATH = `${rootDirectory}/_templates`;

// Grab the path of the user's project
export const pathWhereScriptIsRunning = process.cwd();

// This is where their templates are (at least we assume so)
const theirTemplatePath = pathWhereScriptIsRunning + "/_templates";

const hasTemplates = fs.existsSync(theirTemplatePath);

export const getTemplateLocation = (): string =>
hasTemplates ? theirTemplatePath : DEFAULT_TEMPLATE_PATH;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love how clean this is! 👍 💯 Nice work!

Loading