Skip to content

Commit

Permalink
test: Add smoke tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
darkobits committed Jun 26, 2023
1 parent 21903d8 commit 0bd7596
Show file tree
Hide file tree
Showing 37 changed files with 536 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- name: Build Project
run: npx nr build
- name: Run Tests
run: npx nr test
run: npx nr test.smoke
- name: Release
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
Expand Down
61 changes: 60 additions & 1 deletion nr.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,62 @@
import { nr } from '@darkobits/ts';

export default nr();

export default nr(({ script, command }) => {
script('test.smoke', {
group: 'Testing',
run: [
[
// ----- CJS Tests -----------------------------------------------------

// esbuild
command.node('smoke-test', ['test.js'], {
execaOptions: { cwd: 'smoke-tests/cjs/ts-extension' }
}),

// esbuild
command.node('smoke-test', ['test.js'], {
execaOptions: { cwd: 'smoke-tests/cjs/mts-extension' }
}),

// import()
command.node('smoke-test', ['test.js'], {
execaOptions: { cwd: 'smoke-tests/cjs/mjs-extension' }
}),

// esbuild, issues node:35129 warning
command.node('smoke-test', ['test.js'], {
execaOptions: { cwd: 'smoke-tests/cjs/js-extension' }
}),


// ----- ESM Tests -----------------------------------------------------

// esbuild
command.node('smoke-test', ['test.js'], {
execaOptions: { cwd: 'smoke-tests/esm/ts-extension' }
}),

// import()
command.node('smoke-test', ['test.js'], {
execaOptions: { cwd: 'smoke-tests/esm/js-extension' }
}),

// esbuild, issues node:35129 warning
command.node('smoke-test', ['test.js'], {
execaOptions: { cwd: 'smoke-tests/esm/cjs-extension' }
}),

// esbuild
command.node('smoke-test', ['test.js'], {
execaOptions: { cwd: 'smoke-tests/esm/cts-extension' }
})
]
],
timing: true
});

script('postPrepare', {
run: ['script:test.smoke']
});

});
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"cosmiconfig": "^8.2.0",
"deepmerge": "^4.3.1",
"esbuild": "^0.18.9",
"find-up": "^6.3.0",
"fs-extra": "^11.1.1",
"node-version": "^3.0.0",
"read-pkg-up": "^9.1.0",
Expand Down
6 changes: 6 additions & 0 deletions smoke-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Smoke Tests

These tests are designed to be run _after_ the project has successfully been built. They are designed to
ensure that Saffron can successfully load configuration files in various formats with ESM host projects
and CommonJS host projects. These tests can be run via the `test.smoke` package script and are run
automatically in CI.
3 changes: 3 additions & 0 deletions smoke-tests/cjs/js-extension/app.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
foo: 'bar'
};
4 changes: 4 additions & 0 deletions smoke-tests/cjs/js-extension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "app",
"type": "commonjs"
}
40 changes: 40 additions & 0 deletions smoke-tests/cjs/js-extension/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const os = require('os');

const LogFactory = require('@darkobits/log');

const log = LogFactory({ heading: 'smokeTest' });

/**
* Loading an Implicitly CJS Configuration File in a CJS Project
*
* This tests that:
*
* 1. Saffron (an ESM package) can be dynamically-imported in a CJS package.
* 2. Saffron can locate and parse a configuration file with a .js extension
* where said file will be transpiled to CJS (because this package does not
* declare type:module).
*/
async function implicitCjs() {
try {
const cli = await import('../../../dist/index.js');

cli.command({
handler: ({ config }) => {
if (config && Object.keys(config).length > 0) {
log.verbose(log.prefix('cjs:js-extension'), log.chalk.green('success'));
} else {
throw new Error('No config found.');
}
}
});

cli.init();
} catch (err) {
log.error(log.prefix('cjs:js-extension'), log.chalk.gray(err.message.replaceAll(os.EOL, ' ')));
log.verbose(err.stack);
process.exit(1);
}
}


void implicitCjs();
3 changes: 3 additions & 0 deletions smoke-tests/cjs/mjs-extension/app.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
foo: 'bar'
};
4 changes: 4 additions & 0 deletions smoke-tests/cjs/mjs-extension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "app",
"type": "commonjs"
}
41 changes: 41 additions & 0 deletions smoke-tests/cjs/mjs-extension/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const os = require('os');

const LogFactory = require('@darkobits/log');

const log = LogFactory({ heading: 'smokeTest' });

/**
* Loading an Explicitly ESM Configuration File in a CJS Project
* This tests that:
*
* 1. Saffron (an ESM package) can be dynamically-imported in a CJS package.
* 2. Saffron can locate and parse a configuration file with an .mjs extension
* where said file will be transpiled to ESM.
*
* Note that Saffron should be able to dynamically import this file without
* having to resort to more exotic transpilation strategies.
*/
async function explicitEsm() {
try {
const cli = await import('../../../dist/index.js');

cli.command({
handler: ({ config }) => {
if (config && Object.keys(config).length > 0) {
log.verbose(log.prefix('cjs:mjs-extension'), log.chalk.green('success'));
} else {
throw new Error('No config found.');
}
}
});

cli.init();
} catch (err) {
log.error(log.prefix('cjs:mjs-extension'), log.chalk.gray(err.message.replaceAll(os.EOL, ' ')));
log.verbose(err.stack);
process.exit(1);
}
}


void explicitEsm();
3 changes: 3 additions & 0 deletions smoke-tests/cjs/mts-extension/app.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
foo: 'bar'
};
4 changes: 4 additions & 0 deletions smoke-tests/cjs/mts-extension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "app",
"type": "commonjs"
}
40 changes: 40 additions & 0 deletions smoke-tests/cjs/mts-extension/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const os = require('os');

const LogFactory = require('@darkobits/log');

const log = LogFactory({ heading: 'smokeTest' });

/**
* Loading a TypeScript Configuration File in a CJS Project
*
* This tests that:
*
* 1. Saffron (an ESM package) can be dynamically-imported in a CJS package.
* 2. Saffron can locate and parse a configuration file with an .mts extension
* where said file will be transpiled to ESM (because it has an .mjs
* extension).
*/
async function mtsToCjs() {
try {
const cli = await import('../../../dist/index.js');

cli.command({
handler: ({ config }) => {
if (config && Object.keys(config).length > 0) {
log.verbose(log.prefix('cjs:mts-extension'), log.chalk.green('success'));
} else {
throw new Error('No config found.');
}
}
});

cli.init();
} catch (err) {
log.error(log.prefix('cjs:mts-extension'), log.chalk.gray(err.message.replaceAll(os.EOL, ' ')));
log.verbose(err.stack);
process.exit(1);
}
}


void mtsToCjs();
7 changes: 7 additions & 0 deletions smoke-tests/cjs/mts-extension/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@darkobits/ts/tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "/dev/null"
}
}
3 changes: 3 additions & 0 deletions smoke-tests/cjs/ts-extension/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
foo: 'bar'
};
4 changes: 4 additions & 0 deletions smoke-tests/cjs/ts-extension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "app",
"type": "commonjs"
}
41 changes: 41 additions & 0 deletions smoke-tests/cjs/ts-extension/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const os = require('os');

const LogFactory = require('@darkobits/log');

const log = LogFactory({ heading: 'smokeTest' });


/**
* Loading a TypeScript Configuration File in a CJS Project
*
* This tests that:
*
* 1. Saffron (an ESM package) can be dynamically-imported in a CJS package.
* 2. Saffron can locate and parse a configuration file with a .ts extension
* where said file will be transpiled to CJS (because this package does not
* declare type:module).
*/
async function typeScriptToCjs() {
try {
const cli = await import('../../../dist/index.js');

cli.command({
handler: ({ config }) => {
if (config && Object.keys(config).length > 0) {
log.verbose(log.prefix('cjs:ts-extension'), log.chalk.green('success'));
} else {
throw new Error('No config found.');
}
}
});

cli.init();
} catch (err) {
log.error(log.prefix('cjs:ts-extension'), log.chalk.gray(err.message.replaceAll(os.EOL, ' ')));
log.verbose(err.stack);
process.exit(1);
}
}


void typeScriptToCjs();
7 changes: 7 additions & 0 deletions smoke-tests/cjs/ts-extension/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@darkobits/ts/tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "/dev/null"
}
}
3 changes: 3 additions & 0 deletions smoke-tests/esm/cjs-extension/app.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
foo: 'bar'
};
4 changes: 4 additions & 0 deletions smoke-tests/esm/cjs-extension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "app",
"type": "module"
}
39 changes: 39 additions & 0 deletions smoke-tests/esm/cjs-extension/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os from 'os';

import LogFactory from '@darkobits/log';

import * as cli from '../../../dist/index.js';

const log = LogFactory({ heading: 'smokeTest' });


/**
* Loading an Explicitly ESM Configuration File in a CJS Project
* This tests that:
*
* 1. Saffron can be imported in an ESM package.
* 2. Saffron can locate and parse a configuration file with a .cjs extension
* where said file will be transpiled to CJS.
*/
function explicitCjs() {
try {
cli.command({
handler: ({ config }) => {
if (config && Object.keys(config).length > 0) {
log.verbose(log.prefix('esm:cjs-extension'), log.chalk.green('success'));
} else {
throw new Error('No config found.');
}
}
});

cli.init();
} catch (err) {
log.error(log.prefix('esm:cjs-extension'), log.chalk.gray(err.message.replaceAll(os.EOL, ' ')));
log.verbose(err.stack);
process.exit(1);
}
}


void explicitCjs();
7 changes: 7 additions & 0 deletions smoke-tests/esm/cjs-extension/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@darkobits/ts/tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "/dev/null"
}
}
3 changes: 3 additions & 0 deletions smoke-tests/esm/cts-extension/app.config.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
foo: 'bar'
};
4 changes: 4 additions & 0 deletions smoke-tests/esm/cts-extension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "app",
"type": "module"
}
Loading

0 comments on commit 0bd7596

Please sign in to comment.