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

Snapshots basic support #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.DS_Store
.pingster
31 changes: 29 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ const optimist = require('optimist')
.alias('d', 'verbose')
.alias('h', 'help')
.alias('v', 'version')
.alias('s', 'allow-snapshots')
.describe('c', 'optional path to config file wtih .yml ext')
.describe('d', 'enable verbose logging mode')
.describe('h', 'print help')
.describe('v', 'print version');
.describe('v', 'print version')
.describe('s', 'allow snapshots creation');
const argv = optimist.argv;

if (argv.help) {
Expand All @@ -35,6 +37,7 @@ if (argv.version) {
}

const verboseMode = argv.verbose;
const allowSnapshots = argv.s;

const configPath = path.join(process.cwd(), argv.config || './pingster.yml');
verboseMode && console.log('configPath:', configPath);
Expand All @@ -46,11 +49,25 @@ try {
console.error(`Problem opening config: ${e}`);
return process.exit(1);
}
verboseMode && console.log('config:', JSON.stringify(config, null, 2));

const spinner = ora({ color: 'green' });
spinner.start('Running tests!');

if (allowSnapshots) {
const names = Object.keys(config);
names.forEach(name => {
try {
config[name].snapshot = fs.readJSONSync(
`${path.join(process.cwd(), '.pingster', 's', name)}`
);
} catch (e) {
// 🤔 well it looks like there is no snapshot, or it's broken
}
});
}

verboseMode && console.log('config:', JSON.stringify(config, null, 2));

(async () => {
try {
const results = await tester(config);
Expand All @@ -62,6 +79,16 @@ spinner.start('Running tests!');
} else {
spinner.succeed('All tests have successfully finished!');
}

if (allowSnapshots) {
const successful = results.filter(x => x.success);
successful.forEach(x =>
fs.outputJson(
`${path.join(process.cwd(), '.pingster', 's', x.name)}`,
x.response
)
);
}
} catch (e) {
spinner.fail(`Something went wrong! ${e}`);
}
Expand Down
6 changes: 5 additions & 1 deletion src/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const instance = axios.create({
async function runTest(test) {
try {
const { status, data, headers } = await instance.get(test.url);
const { expect } = test;
const { expect, snapshot } = test;

let success = true; // if there is no expects and request succeeded - all good ^_^

Expand All @@ -27,6 +27,10 @@ async function runTest(test) {
}
}

if (snapshot) {
success = success && ismatch(data, snapshot.data);
}

return { success, response: { status, data, headers }, ...test };
} catch (e) {
return { success: false, error: e, ...test };
Expand Down