Skip to content

Commit

Permalink
refactor: minor dx improvements
Browse files Browse the repository at this point in the history
refactor: minor dx improvements
  • Loading branch information
giulianok authored Apr 22, 2024
1 parent 1afc38f commit 3be9b05
Show file tree
Hide file tree
Showing 19 changed files with 6,347 additions and 19,000 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [ '12.x', '14.x', '16.x', '18.x' ]
node: [ '18.x', '20.x' ]
name: Node ${{ matrix.node }}
steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20
61 changes: 57 additions & 4 deletions __tests__/bin/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* eslint-disable eslint-comments/disable-enable-pair -- to exclude rule for the entire file */
/* eslint-disable no-underscore-dangle -- used for custom functions needed just for testing */
const generateFromTemplate = require('../../src/generate-from-template');
const prompts = require('../../src/utils/prompts');

jest.mock('../../src/generate-from-template', () => jest.fn());
jest.mock('../../src/utils/create-build-logger', () => () => ({
Expand All @@ -8,11 +11,29 @@ jest.mock('../../src/utils/create-build-logger', () => () => ({
init: jest.fn(),
moveBuildLogToProject: jest.fn(),
}));
jest.mock('../../src/utils/prompts', () => {
let aborted = false;

return {
prompts: jest.fn(),
hasTheUserAborted: () => aborted,
_setAborted: (value) => {
aborted = value;
},
};
});

const waitToResolve = () => new Promise((resolve) => {
setTimeout(() => {
resolve();
});
});

describe('bin', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should bootstrap the generation process by passing the cli parameters to generateFromTemplate', () => {
process.argv = ['node', 'create-using-template', 'templateNameMock', '--projectName', 'mockProjectName'];
jest.isolateModules(() => {
Expand All @@ -32,16 +53,23 @@ describe('bin', () => {
},
});
});
it('should catch and log exceptions thrown during generation', () => {

it('should catch and log exceptions thrown during generation', async () => {
process.argv = ['node', 'create-using-template', 'templateNameMock'];
generateFromTemplate.mockImplementationOnce(() => new Promise((_, reject) => reject(new Error('ErrorMock'))));
jest.spyOn(console, 'error').mockImplementationOnce(() => {}).mockImplementationOnce(() => {});
jest.spyOn(process, 'exit').mockImplementationOnce(() => {});
const errorMock = new Error('ErrorMock');
generateFromTemplate.mockImplementationOnce(() => new Promise((_, reject) => {
reject(errorMock);
}));
jest.spyOn(console, 'error').mockImplementationOnce(() => { }).mockImplementationOnce(() => { });
jest.spyOn(process, 'exit').mockImplementationOnce(() => { });

jest.isolateModules(() => {
// eslint-disable-next-line global-require -- we are testing `import time` code
require('../../bin');
});

await waitToResolve();

expect(generateFromTemplate).toHaveBeenCalledTimes(1);
expect(generateFromTemplate).toHaveBeenNthCalledWith(1, {
templateName: 'templateNameMock',
Expand All @@ -54,5 +82,30 @@ describe('bin', () => {
},
options: {},
});
expect(console.error).toHaveBeenCalledTimes(2);
expect(console.error).toHaveBeenCalledWith('Failed to create module:', 'ErrorMock');
expect(console.error).toHaveBeenCalledWith(errorMock);
});

it('logs "bye bye" when user aborts / exists process', async () => {
prompts._setAborted(true);

process.argv = ['node', 'create-using-template', 'templateNameMock'];

generateFromTemplate.mockImplementationOnce(() => new Promise((_, reject) => { reject(new Error('ErrorMock')); }));

jest.spyOn(console, 'error').mockImplementationOnce(() => { });
jest.spyOn(console, 'log').mockImplementationOnce(() => { });
jest.spyOn(process, 'exit').mockImplementationOnce(() => { });

jest.isolateModules(() => {
// eslint-disable-next-line global-require -- we are testing `import time` code
require('../../bin');
});

await waitToResolve();

expect(generateFromTemplate).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith('Template generation aborted');
});
});
4 changes: 3 additions & 1 deletion __tests__/generate-from-template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const { initializeGitRepo, createInitialCommit } = require('../src/utils/git');
const generateFromTemplate = require('../src/generate-from-template');
const renameDirectories = require('../src/utils/renameDirectories');

jest.mock('prompts', () => 'promptsMock');
jest.mock('../src/utils/prompts', () => ({
prompts: 'promptsMock',
}));
jest.mock('../src/utils/log', () => ({
goToStep: jest.fn(),
}));
Expand Down
10 changes: 7 additions & 3 deletions __tests__/utils/__snapshots__/get-base-options.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getBaseOptions should call prompts with the correct set of base options 1`] = `
Array [
Array [
Object {
[
[
{
"initial": "",
"message": "Enter your project's name. This will also be used as the directory name for the project:",
"name": "projectName",
"type": "text",
"validate": [Function],
},
],
{
"onCancel": [Function],
},
]
`;
42 changes: 21 additions & 21 deletions __tests__/utils/__snapshots__/git.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`createInitialCommit commits a message and a branch from the special template values 1`] = `
Array [
Array [
[
[
"git",
Array [
[
"add",
".",
],
"repoPathMock",
],
Array [
[
"git",
Array [
[
"commit",
"-minitialCommitMessageMock",
"--quiet",
],
"repoPathMock",
],
Array [
[
"git",
Array [
[
"branch",
"-m",
"defaultBranchNameMock",
Expand All @@ -32,27 +32,27 @@ Array [
`;

exports[`createInitialCommit commits a message on the main branch 1`] = `
Array [
Array [
[
[
"git",
Array [
[
"add",
".",
],
"repoPathMock",
],
Array [
[
"git",
Array [
[
"commit",
"-mfeat(generation): initial commit",
"--quiet",
],
"repoPathMock",
],
Array [
[
"git",
Array [
[
"branch",
"-m",
"main",
Expand All @@ -63,28 +63,28 @@ Array [
`;

exports[`createInitialCommit includes initialCommitOptions 1`] = `
Array [
Array [
[
[
"git",
Array [
[
"add",
".",
],
"repoPathMock",
],
Array [
[
"git",
Array [
[
"commit",
"-mfeat(generation): initial commit",
"--quiet",
"--test-option",
],
"repoPathMock",
],
Array [
[
"git",
Array [
[
"branch",
"-m",
"defaultBranchNameMock",
Expand Down
36 changes: 18 additions & 18 deletions __tests__/utils/__snapshots__/log.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ exports[`log functions stepBanner outputs correct string for step 1 1`] = `
_ _ _ _____ _ _
| | | |___(_)_ __ __ _ |_ _|__ _ __ ___ _ __ | | __ _| |_ ___
| | | / __| | '_ \\\\ / _\` | | |/ _ \\\\ '_ \` _ \\\\| '_ \\\\| |/ _\` | __/ _ \\\\
| |_| \\\\__ \\\\ | | | | (_| | | | __/ | | | | | |_) | | (_| | || __/
\\\\___/|___/_|_| |_|\\\\__, | |_|\\\\___|_| |_| |_| .__/|_|\\\\__,_|\\\\__\\\\___|
| | | / __| | '_ \\ / _\` | | |/ _ \\ '_ \` _ \\| '_ \\| |/ _\` | __/ _ \\
| |_| \\__ \\ | | | | (_| | | | __/ | | | | | |_) | | (_| | || __/
\\___/|___/_|_| |_|\\__, | |_|\\___|_| |_| |_| .__/|_|\\__,_|\\__\\___|
|___/ |_| vpackageVersionMock
Step 1 -> Step 2 -> Step 3 -> Step 4 -> Step 5 -> Step 6
Expand All @@ -20,9 +20,9 @@ exports[`log functions stepBanner outputs correct string for step 2 1`] = `
_ _ _ _____ _ _
| | | |___(_)_ __ __ _ |_ _|__ _ __ ___ _ __ | | __ _| |_ ___
| | | / __| | '_ \\\\ / _\` | | |/ _ \\\\ '_ \` _ \\\\| '_ \\\\| |/ _\` | __/ _ \\\\
| |_| \\\\__ \\\\ | | | | (_| | | | __/ | | | | | |_) | | (_| | || __/
\\\\___/|___/_|_| |_|\\\\__, | |_|\\\\___|_| |_| |_| .__/|_|\\\\__,_|\\\\__\\\\___|
| | | / __| | '_ \\ / _\` | | |/ _ \\ '_ \` _ \\| '_ \\| |/ _\` | __/ _ \\
| |_| \\__ \\ | | | | (_| | | | __/ | | | | | |_) | | (_| | || __/
\\___/|___/_|_| |_|\\__, | |_|\\___|_| |_| |_| .__/|_|\\__,_|\\__\\___|
|___/ |_| vpackageVersionMock
Step 1 -> Step 2 -> Step 3 -> Step 4 -> Step 5 -> Step 6
Expand All @@ -35,9 +35,9 @@ exports[`log functions stepBanner outputs correct string for step 3 1`] = `
_ _ _ _____ _ _
| | | |___(_)_ __ __ _ |_ _|__ _ __ ___ _ __ | | __ _| |_ ___
| | | / __| | '_ \\\\ / _\` | | |/ _ \\\\ '_ \` _ \\\\| '_ \\\\| |/ _\` | __/ _ \\\\
| |_| \\\\__ \\\\ | | | | (_| | | | __/ | | | | | |_) | | (_| | || __/
\\\\___/|___/_|_| |_|\\\\__, | |_|\\\\___|_| |_| |_| .__/|_|\\\\__,_|\\\\__\\\\___|
| | | / __| | '_ \\ / _\` | | |/ _ \\ '_ \` _ \\| '_ \\| |/ _\` | __/ _ \\
| |_| \\__ \\ | | | | (_| | | | __/ | | | | | |_) | | (_| | || __/
\\___/|___/_|_| |_|\\__, | |_|\\___|_| |_| |_| .__/|_|\\__,_|\\__\\___|
|___/ |_| vpackageVersionMock
Step 1 -> Step 2 -> Step 3 -> Step 4 -> Step 5 -> Step 6
Expand All @@ -50,9 +50,9 @@ exports[`log functions stepBanner outputs correct string for step 4 1`] = `
_ _ _ _____ _ _
| | | |___(_)_ __ __ _ |_ _|__ _ __ ___ _ __ | | __ _| |_ ___
| | | / __| | '_ \\\\ / _\` | | |/ _ \\\\ '_ \` _ \\\\| '_ \\\\| |/ _\` | __/ _ \\\\
| |_| \\\\__ \\\\ | | | | (_| | | | __/ | | | | | |_) | | (_| | || __/
\\\\___/|___/_|_| |_|\\\\__, | |_|\\\\___|_| |_| |_| .__/|_|\\\\__,_|\\\\__\\\\___|
| | | / __| | '_ \\ / _\` | | |/ _ \\ '_ \` _ \\| '_ \\| |/ _\` | __/ _ \\
| |_| \\__ \\ | | | | (_| | | | __/ | | | | | |_) | | (_| | || __/
\\___/|___/_|_| |_|\\__, | |_|\\___|_| |_| |_| .__/|_|\\__,_|\\__\\___|
|___/ |_| vpackageVersionMock
Step 1 -> Step 2 -> Step 3 -> Step 4 -> Step 5 -> Step 6
Expand All @@ -65,9 +65,9 @@ exports[`log functions stepBanner outputs correct string for step 5 1`] = `
_ _ _ _____ _ _
| | | |___(_)_ __ __ _ |_ _|__ _ __ ___ _ __ | | __ _| |_ ___
| | | / __| | '_ \\\\ / _\` | | |/ _ \\\\ '_ \` _ \\\\| '_ \\\\| |/ _\` | __/ _ \\\\
| |_| \\\\__ \\\\ | | | | (_| | | | __/ | | | | | |_) | | (_| | || __/
\\\\___/|___/_|_| |_|\\\\__, | |_|\\\\___|_| |_| |_| .__/|_|\\\\__,_|\\\\__\\\\___|
| | | / __| | '_ \\ / _\` | | |/ _ \\ '_ \` _ \\| '_ \\| |/ _\` | __/ _ \\
| |_| \\__ \\ | | | | (_| | | | __/ | | | | | |_) | | (_| | || __/
\\___/|___/_|_| |_|\\__, | |_|\\___|_| |_| |_| .__/|_|\\__,_|\\__\\___|
|___/ |_| vpackageVersionMock
Step 1 -> Step 2 -> Step 3 -> Step 4 -> Step 5 -> Step 6
Expand All @@ -80,9 +80,9 @@ exports[`log functions stepBanner outputs correct string for step 6 1`] = `
_ _ _ _____ _ _
| | | |___(_)_ __ __ _ |_ _|__ _ __ ___ _ __ | | __ _| |_ ___
| | | / __| | '_ \\\\ / _\` | | |/ _ \\\\ '_ \` _ \\\\| '_ \\\\| |/ _\` | __/ _ \\\\
| |_| \\\\__ \\\\ | | | | (_| | | | __/ | | | | | |_) | | (_| | || __/
\\\\___/|___/_|_| |_|\\\\__, | |_|\\\\___|_| |_| |_| .__/|_|\\\\__,_|\\\\__\\\\___|
| | | / __| | '_ \\ / _\` | | |/ _ \\ '_ \` _ \\| '_ \\| |/ _\` | __/ _ \\
| |_| \\__ \\ | | | | (_| | | | __/ | | | | | |_) | | (_| | || __/
\\___/|___/_|_| |_|\\__, | |_|\\___|_| |_| |_| .__/|_|\\__,_|\\__\\___|
|___/ |_| vpackageVersionMock
Step 1 -> Step 2 -> Step 3 -> Step 4 -> Step 5 -> Step 6
Expand Down
Loading

0 comments on commit 3be9b05

Please sign in to comment.