Skip to content
generated from puria/README

Commit

Permalink
test: more json coverage test (#16)
Browse files Browse the repository at this point in the history
* test: more json coverage test

* chore: exclude *.spec.ts from coverage report
matteo-cristino authored Jan 27, 2025
1 parent 6b87b6f commit 129948a
Showing 2 changed files with 72 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@
"diff-integration-tests": "mkdir -p diff && rm -rf diff/test && cp -r test diff/test && rm -rf diff/test/test-*/.git && cd diff && git init --quiet && git add -A && git commit --quiet --no-verify --allow-empty -m 'WIP' && echo '\\n\\nCommitted most recent integration test output in the \"diff\" directory. Review the changes with \"cd diff && git diff HEAD\" or your preferred git diff viewer.'",
"watch:build": "tsc -p tsconfig.json -w",
"watch:test": "nyc --silent ava --watch",
"coverage": "pnpm build && c8 -r text -r lcov -o .coverage pnpm test:unit",
"coverage": "pnpm build && c8 -r text -r lcov -o .coverage --exclude '**/*.spec.*' pnpm test:unit",
"doc": "run-s doc:html && open-cli build/docs/index.html",
"doc:html": "typedoc src/ --exclude **/*.spec.ts --out build/docs",
"doc:json": "typedoc src/ --exclude **/*.spec.ts --target ES6 --mode file --json build/docs/typedoc.json",
78 changes: 71 additions & 7 deletions src/lib/chain.spec.ts
Original file line number Diff line number Diff line change
@@ -6,8 +6,11 @@ import test from 'ava';

import { execute } from './chain.js';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(global as any).variable = 'Hello';
declare global {
// eslint-disable-next-line no-var
var variable: string;
}
global.variable = 'Hello';

test('should execute work [yaml]', async (t) => {
const account = JSON.stringify({ username: 'Alice' });
@@ -522,24 +525,24 @@ test('mix zencode and zencodeFromFile', async (t) => {
});
});

test('onBefore create a file and delete it onAfter', async (t) => {
test('onBefore create a file and delete it onAfter [yaml]', async (t) => {
process.env['FILES_DIR'] = '.';
const steps = `
steps:
- id: create and delete new_file
onBefore:
run: |
touch new_file.test
touch new_file_yaml.test
zencode: |
Rule unknown ignore
Given I send path 'file_path' and verify file exists
Given I have a 'string' named 'file_path'
Then print the 'file_path'
keys:
file_path: new_file.test
file_path: new_file_yaml.test
onAfter:
run: |
rm new_file.test
rm new_file_yaml.test
- id: check new_file does not exist
dataFromStep: create and delete new_file
zencode: |
@@ -553,9 +556,40 @@ test('onBefore create a file and delete it onAfter', async (t) => {
});
});

test('onBefore create a file and delete it onAfter [json]', async (t) => {
process.env['FILES_DIR'] = '.';
const steps = {
steps: [
{
id: 'create and delete new_file',
onBefore: {
run: 'touch new_file_json.test',
},
zencode:
"Rule unknown ignore\nGiven I send path 'file_path' and verify file exists\nGiven I have a 'string' named 'file_path'\nThen print the 'file_path'\n",
keys: '{"file_path": "new_file_json.test"}',
onAfter: {
run: 'rm new_file_json.test',
},
},
{
id: 'check new_file does not exist',
dataFromStep: 'create and delete new_file',
zencode:
"Rule unknown ignore\nGiven I send path 'file_path' and verify file does not exist\nGiven I have a 'string' named 'file_path'\nThen print the string 'everything works'\n",
},
],
};
const result = await execute(steps);
console.log(result);
t.deepEqual(JSON.parse(result), {
output: ['everything_works'],
});
});

// failing tests

test('check for variables onBefore and onAfter, pass onBefore and fails onAfter', async (t) => {
test('check for variables onBefore and onAfter, pass onBefore and fails onAfter [yaml]', async (t) => {
const steps = `
steps:
- id: create and delete new_file
@@ -588,6 +622,36 @@ Error: result is not Hello_world: Hello_cat`,
);
});

test('check for variables onBefore and onAfter, pass onBefore and fails onAfter [json]', async (t) => {
const steps = {
steps: [
{
id: 'create and delete new_file',
onBefore: {
jsFunction: () => {
if (variable !== 'Hello') {
throw new Error('variable is not Hello: ' + variable);
}
},
},
zencode:
"Given nothing\nWhen I set 'res' to 'cat' as 'string'\nThen print the 'res'\n",
onAfter: {
jsFunction: (result: string) => {
const r = variable + '_' + JSON.parse(result).res;
if (r !== 'Hello_world') {
throw new Error('result is not Hello_world: ' + r);
}
},
},
},
],
};
const fn = execute(steps);
const err = await t.throwsAsync(fn);
t.is(err?.message, 'result is not Hello_world: Hello_cat');
});

test('invalid data', async (t) => {
const steps = `
steps:

0 comments on commit 129948a

Please sign in to comment.