Skip to content

Commit

Permalink
chore: fix a load of tests
Browse files Browse the repository at this point in the history
add tests for various config file formats
  • Loading branch information
boneskull committed Jan 3, 2024
1 parent cac79c0 commit dc11cc0
Show file tree
Hide file tree
Showing 24 changed files with 161 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports['midnight-smoker [E2E] built-in checks when a check fails when the rule severity is "error" should produce expected output [snapshot] 1'] = `
exports['midnight-smoker [E2E] linting when a rule fails when the rule severity is "error" should produce expected output [snapshot] 1'] = `
💨 midnight-smoker@<version>
- Packing current project…
Expand All @@ -12,7 +12,7 @@ exports['midnight-smoker [E2E] built-in checks when a check fails when the rule
✖ 🤮 Maurice!
`

exports['midnight-smoker [E2E] built-in checks when a check fails when the rule severity is "warn" should produce expected output [snapshot] 1'] = `
exports['midnight-smoker [E2E] linting when a rule fails when the rule severity is "warn" should produce expected output [snapshot] 1'] = `
💨 midnight-smoker@<version>
- Packing current project…
Expand All @@ -26,7 +26,7 @@ exports['midnight-smoker [E2E] built-in checks when a check fails when the rule
✔ Lovey-dovey! 💖
`

exports['midnight-smoker [E2E] built-in checks when a check fails when the rule severity is "off" should produce expected output [snapshot] 1'] = `
exports['midnight-smoker [E2E] linting when a rule fails when the rule severity is "off" should produce expected output [snapshot] 1'] = `
💨 midnight-smoker@<version>
- Packing current project…
Expand Down
2 changes: 1 addition & 1 deletion packages/midnight-smoker/src/cli/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import Debug from 'debug';
import type {InferredOptionTypes, Options} from 'yargs';
import {DEFAULT_PACKAGE_MANAGER_SPEC} from '../constants';

export const BEHAVIOR_GROUP = 'Script Behavior:';
export const OUTPUT_GROUP = 'Output:';
export const INPUT_GROUP = 'Input:';

/**
* Reusable config for array-type options
*/
Expand Down
13 changes: 10 additions & 3 deletions packages/midnight-smoker/src/cli/command/run-script.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Defines the `run-script` command using {@link RunScriptCommand}
*
* @packageDocumentation
*/

import Debug from 'debug';
import type {
ArgumentsCamelCase,
Expand All @@ -8,12 +14,14 @@ import type {
import {castArray} from '../../schema-util';
import {Smoker} from '../../smoker';
import type {CommonOptionTypes, GlobalOptionTypes} from '../cli-options';
import {ARRAY_OPT_CFG, BEHAVIOR_GROUP, CommonOptions} from '../cli-options';
import {ARRAY_OPT_CFG, CommonOptions} from '../cli-options';
import {handleRejection} from '../cli-util';
import {BaseCommand} from './base';

const debug = Debug('midnight-smoker:cli:lint');

const BEHAVIOR_GROUP = 'Script Behavior:';

const RunScriptOptions = {
...CommonOptions,
add: {
Expand All @@ -35,10 +43,9 @@ const RunScriptOptions = {
},
loose: {
alias: 'if-present',
describe: 'Ignore missing scripts (used with --all)',
describe: 'Ignore missing scripts (use with workspaces)',
boolean: true,
group: BEHAVIOR_GROUP,
implies: 'all',
},
} as const satisfies Record<string, Options>;

Expand Down
44 changes: 22 additions & 22 deletions packages/midnight-smoker/src/config-file.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import Debug from 'debug';
import type {Loader, type Options as LilconfigOpts} from 'lilconfig';
import type {Options as LilconfigOpts, Loader} from 'lilconfig';
import {lilconfig} from 'lilconfig';
import {justImport, importTs as loadTs} from './loader-util';
import type {RawSmokerOptions} from './options/options';
import {toDualCasedObject} from './schema-util';

const debug = Debug('midnight-smoker:config-file');

export const loadEsm: Loader = async (filepath): Promise<unknown> => {
/**
* JavaScript loader for `lilconfig`
*
* @param filepath - Path to file to load
* @returns Whatever it is
*/
const loadEsm: Loader = async (filepath): Promise<unknown> => {
return await justImport(filepath);
};

const BASE_CFG_FILENAMES = [
'.smokerrc',
'smoker.config',
'.config/smokerrc',
'.config/smoker.config',
] as const;

const DEFAULT_OPTS: Readonly<LilconfigOpts> = Object.freeze({
loaders: {'.mjs': loadEsm, '.js': loadEsm, '.ts': loadTs},
searchPlaces: [
'package.json',
'.smokerrc.json',
'.smokerrc.js',
'.smokerrc.cjs',
'.smokerrc.mjs',
'.smokerrc.ts',
'smoker.config.json',
'smoker.config.js',
'smoker.config.cjs',
'smoker.config.mjs',
'smoker.config.ts',
'.config/smokerrc.json',
'.config/smokerrc.js',
'.config/smokerrc.cjs',
'.config/smokerrc.mjs',
'.config/smokerrc.ts',
'.config/smoker.config.json',
'.config/smoker.config.js',
'.config/smoker.config.cjs',
'.config/smoker.config.mjs',
'.config/smoker.config.ts',
...BASE_CFG_FILENAMES.flatMap((name) => [
`${name}.json`,
`${name}.js`,
`${name}.cjs`,
`${name}.mjs`,
`${name}.ts`,
]),
],
});

Expand Down
102 changes: 67 additions & 35 deletions packages/midnight-smoker/test/e2e/config-file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('midnight-smoker [E2E]', function () {
const cwd = path.join(__dirname, 'fixture', 'config-file', 'config-esm');

it('should respect the config file', async function () {
const {stdout} = await execSmoker(['run', '--no-lint'], {
const {stdout} = await execSmoker(['run', 'smoke', '--no-lint'], {
cwd,
});
const result = JSON.parse(stdout);
Expand All @@ -28,16 +28,11 @@ describe('midnight-smoker [E2E]', function () {
});
});

describe('when config is within package.json', function () {
const cwd = path.join(
__dirname,
'fixture',
'config-file',
'config-package-json',
);
describe('when config file is TS', function () {
const cwd = path.join(__dirname, 'fixture', 'config-file', 'config-ts');

it('should respect the config file', async function () {
const {stdout} = await execSmoker(['smoke'], {
const {stdout} = await execSmoker(['run', 'smoke', '--no-lint'], {
cwd,
});
const result = JSON.parse(stdout);
Expand All @@ -54,59 +49,96 @@ describe('midnight-smoker [E2E]', function () {
});
});

describe('when an config file contains a "script" prop', function () {
const cwd = path.join(
__dirname,
'fixture',
'config-file',
'config-script',
);
describe('when config file is CJS', function () {
const cwd = path.join(__dirname, 'fixture', 'config-file', 'config-cjs');

it('should run script from config file', async function () {
// includes json: true
const {stdout} = await execSmoker(['--no-lint'], {cwd});
it('should respect the config file', async function () {
const {stdout} = await execSmoker(['run', 'smoke', '--no-lint'], {
cwd,
});
const result = JSON.parse(stdout);
expect(result, 'to satisfy', {
results: {
scripts: expect.it('to have length', 1),
scripts: expect
.it('to have length', 2)
.and('to satisfy', [
{rawResult: {command: /npm/}},
{rawResult: {command: /yarn/}},
]),
},
});
});
});

describe('when the CLI also contains a script', function () {
it('should run all scripts', async function () {
const {stdout} = await execSmoker(['run', '--no-lint'], {cwd});
const result = JSON.parse(stdout);
expect(result, 'to satisfy', {
results: {
scripts: expect.it('to have length', 2),
},
});
describe('when config file is JSON', function () {
const cwd = path.join(__dirname, 'fixture', 'config-file', 'config-json');

it('should respect the config file', async function () {
const {stdout} = await execSmoker(['run', 'smoke', '--no-lint'], {
cwd,
});
const result = JSON.parse(stdout);
expect(result, 'to satisfy', {
results: {
scripts: expect
.it('to have length', 2)
.and('to satisfy', [
{rawResult: {command: /npm/}},
{rawResult: {command: /yarn/}},
]),
},
});
});
});

describe('when an config file contains a "scripts" prop', function () {
describe('when config is within package.json', function () {
const cwd = path.join(
__dirname,
'fixture',
'config-file',
'config-scripts',
'config-package-json',
);

it('should run scripts from config file', async function () {
const {stdout} = await execSmoker(['--no-lint'], {cwd});
it('should respect the config file', async function () {
const {stdout} = await execSmoker(['run', 'smoke'], {
cwd,
});
const result = JSON.parse(stdout);
expect(result, 'to satisfy', {
results: {
scripts: expect.it('to have length', 1),
scripts: expect
.it('to have length', 2)
.and('to satisfy', [
{rawResult: {command: /npm/}},
{rawResult: {command: /yarn/}},
]),
},
});
});
});

describe('when an config file contains a "script" prop', function () {
const cwd = path.join(
__dirname,
'fixture',
'config-file',
'config-script',
);

it('should still fail if no script argument is provided', async function () {
// includes json: true
await expect(
execSmoker(['run', '--no-lint'], {cwd}),
'to be rejected with error satisfying',
{stderr: /not enough non-option arguments/i},
);
});

describe('when the CLI also contains a script', function () {
it('should run all scripts', async function () {
const {stdout} = await execSmoker(['run', '--no-lint'], {cwd});
const {stdout} = await execSmoker(['run', 'smoke', '--no-lint'], {
cwd,
});
const result = JSON.parse(stdout);
expect(result, 'to satisfy', {
results: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "config-cjs",
"version": "1.0.0",
"scripts": {
"smoke": "exit 0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
pkgManager: ['npm@latest', 'yarn@1'],
json: true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"pkgManager": ["npm@latest", "yarn@1"],
"json": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "config-json",
"version": "1.0.0",
"scripts": {
"smoke": "exit 0"
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "config-ts",
"version": "1.0.0",
"scripts": {
"smoke": "exit 0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
pkgManager: ['npm@latest', 'yarn@1'],
json: true
}
27 changes: 20 additions & 7 deletions packages/midnight-smoker/test/e2e/general.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ describe('midnight-smoker [E2E]', function () {
);

it('should use the default package manager', async function () {
const {stdout} = await execSmoker(['run', '--json', '--no-lint'], {
cwd,
});
const {stdout} = await execSmoker(
['run', 'smoke', '--json', '--no-lint'],
{
cwd,
},
);
const {results} = JSON.parse(fixupOutput(stdout, false));
expect(results, 'to satisfy', {
scripts: expect
Expand All @@ -67,9 +70,12 @@ describe('midnight-smoker [E2E]', function () {
});

it('should use the default package manager', async function () {
const {stdout} = await execSmoker(['run', '--json', '--no-lint'], {
cwd,
});
const {stdout} = await execSmoker(
['run', 'smoke', '--json', '--no-lint'],
{
cwd,
},
);
const {results} = JSON.parse(fixupOutput(stdout, false));
expect(results, 'to satisfy', {
scripts: expect
Expand Down Expand Up @@ -127,7 +133,14 @@ describe('midnight-smoker [E2E]', function () {

before(async function () {
const {stdout, failed: f} = await execSmoker(
['run', '--add=cross-env', '--linger', '--no-lint', '--json'],
[
'run',
'smoke',
'--add=cross-env',
'--linger',
'--no-lint',
'--json',
],
{
cwd,
},
Expand Down
Loading

0 comments on commit dc11cc0

Please sign in to comment.