Skip to content

Commit

Permalink
chore(testing): add e2e testing
Browse files Browse the repository at this point in the history
  • Loading branch information
aorinevo committed Oct 6, 2024
1 parent fa776f9 commit 4cbdc1d
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 2 deletions.
81 changes: 81 additions & 0 deletions e2e/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CLI End-to-End Tests should display help information 1`] = `
"Usage: cli [options] [command]
Options:
-h, --help display help for command
Commands:
checkout [options] <migration> Check out any repositories that are
candidates for a given migration
apply [options] <migration> Apply a migration to all checked out
repositories
commit [options] <migration> Commit all changes for the specified
migration
reset [options] <migration> Reset all changes for the specified
migration
push [options] <migration> Push all changes for the specified
migration
pr-preview [options] <migration> View a preview of the PR messages for the
specified migration
pr [options] <migration> Create PRs for the specified migration
pr-status [options] <migration> Check the status of all PRs for the
specified migration
list <migration> List all checked out repositories for the
given migration
version Print Shepherd version
help [command] display help for command
"
`;
exports[`CLI End-to-End Tests should successfully checkout using repos flag 1`] = `
"Using 2 selected repos
[aorinevo/shepherd-test-repo-1] 1/2
> Running should_migrate steps
✔ Completed all should_migrate steps successfully
> Running post_checkout steps
✔ Completed all post_checkout steps successfully
[aorinevo/shepherd-test-repo-2] 2/2
> Running should_migrate steps
✔ Completed all should_migrate steps successfully
> Running post_checkout steps
✔ Completed all post_checkout steps successfully
Checked out 2 out of 2 repos
"
`;
exports[`CLI End-to-End Tests should successfully run apply on a migration 1`] = `
"
[NerdWalletOSS/shepherd] 1/1
ℹ Running apply steps
$ touch $SHEPHERD_REPO_DIR/testfile.js && echo "some content" > $SHEPHERD_REPO_DIR/testfile.js
Step "touch $SHEPHERD_REPO_DIR/testfile.js && echo "some content" > $SHEPHERD_REPO_DIR/testfile.js" exited with 0
✔ Completed all apply steps successfully
"
`;
exports[`CLI End-to-End Tests should successfully run apply on a migration 2`] = `
"On branch 2024.10.06-test-migration
Untracked files:
(use "git add <file>..." to include in what will be committed)
testfile.js
nothing added to commit but untracked files present (use "git add" to track)
"
`;
exports[`CLI End-to-End Tests should successfully run checkout on a migration 1`] = `
"
[NerdWalletOSS/shepherd] 1/1
> Running should_migrate steps
✔ Completed all should_migrate steps successfully
> Running post_checkout steps
✔ Completed all post_checkout steps successfully
Checked out 1 out of 1 repos
"
`;
10 changes: 10 additions & 0 deletions e2e/assets/checkout-apply/shepherd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
id: 2024.10.06-test-migration
title: |
feat: test migration
adapter:
type: github
search_query: repo:NerdWalletOSS/shepherd path:/
hooks:
apply: 'touch $SHEPHERD_REPO_DIR/testfile.js && echo "some content" > $SHEPHERD_REPO_DIR/testfile.js'
pr_message: |
echo "e2e test suite"
54 changes: 54 additions & 0 deletions e2e/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { execSync } from 'child_process';
import path from 'path';
import fs from 'fs';
import os from 'os';

describe('CLI End-to-End Tests', () => {
let shepherdRepoDir: string;

beforeAll(() => {
shepherdRepoDir = path.resolve(
os.homedir(),
'.shepherd',
'2024.10.06-test-migration',
'repos',
'NerdWalletOSS',
'shepherd'
);
});

const cliPath = path.resolve(__dirname, '../lib/cli.js');

const runCLI = (args: string) => {
return execSync(`node ${cliPath} ${args}`, { encoding: 'utf-8' });
};

it('should display help information', async () => {
const output = runCLI('--help');
expect(output).toMatchSnapshot();
});

it('should display version information', async () => {
const output = runCLI('version');
expect(output.split('.').length).toEqual(3);
});

it('should successfully run checkout on a migration', async () => {
const output = runCLI(`checkout ${path.join(__dirname, './assets/checkout-apply')}`);
expect(output).toMatchSnapshot();
});

it('should successfully run apply on a migration', async () => {
const output = runCLI(`apply ${path.join(__dirname, './assets/checkout-apply')}`);
expect(output).toMatchSnapshot();
const gitDiffOutput = execSync(`cd ${shepherdRepoDir} && git status`, { encoding: 'utf-8' });
expect(gitDiffOutput).toMatchSnapshot();
});

it('should successfully checkout using repos flag', async () => {
const output = runCLI(
`checkout --repos "aorinevo/shepherd-test-repo-1,aorinevo/shepherd-test-repo-2" ${path.join(__dirname, './assets/checkout-apply')}`
);
expect(output).toMatchSnapshot();
});
});
2 changes: 1 addition & 1 deletion jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
extensionsToTreatAsEsm: ['.ts', '.mts'],
coveragePathIgnorePatterns: ['\\.mock\\.ts$'],
moduleFileExtensions: ['ts', 'mts', 'tsx', 'js', 'mjs', 'jsx', 'json', 'node'],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?|mts)$',
testRegex: '(/(__tests__|e2e)/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?|mts)$',
transform: {
'^.+\\.(ts|mts|tsx|js|mjs)?$': 'babel-jest',
'^.+\\.js$': 'babel-jest',
Expand Down
19 changes: 19 additions & 0 deletions jest.e2e.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
extensionsToTreatAsEsm: ['.ts', '.mts'],
coveragePathIgnorePatterns: ['\\.mock\\.ts$'],
moduleFileExtensions: ['ts', 'mts', 'tsx', 'js', 'mjs', 'jsx', 'json', 'node'],
testRegex: '(/(__tests__|e2e)/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?|mts)$',
transform: {
'^.+\\.(ts|mts|tsx|js|mjs)?$': 'babel-jest',
'^.+\\.js$': 'babel-jest',
},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1', // Map ".js" to ".ts" in imports
},
transformIgnorePatterns: [
'/node_modules/(?!(@octokit|before-after-hook|universal-user-agent|bottleneck|@octokit/core|@octokit/plugin-retry|@octokit/plugin-rest-endpoint-methods|@octokit/plugin-throttling|chalk)/)', // Ensure these node_modules are transformed
],
collectCoverageFrom: ['src/**/*.{ts,tsx,js,jsx}'], // Collect coverage from src directory
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"fix-lint": "eslint src/**/*.ts --fix && prettier --write .",
"lint": "eslint src/**/*.ts && prettier --check .",
"prepublishOnly": "yarn test && yarn build",
"test": "jest --coverage src/"
"test": "jest --coverage src/",
"e2e-test": "npm run build && jest --config=jest.e2e.config.cjs e2e/"
},
"files": [
"lib"
Expand Down

0 comments on commit 4cbdc1d

Please sign in to comment.