Skip to content

Commit

Permalink
test: add support for doing snapshot testing
Browse files Browse the repository at this point in the history
Snapshot testing is one of the idiomatic way of producing
tests while building a parser.

Our snapshot tests will be of two types
a.) Dehydrated ApiDOM snapshot testing

Best for keeping track of data and metadata encoded
within ApiDOM.

b.) S-expresssion representation of ApiDOM

Best for checking if semantics of ApiDOM Tree
are correct.

Closes #432
  • Loading branch information
char0n committed Jun 2, 2021
1 parent 305e1c9 commit ac762f9
Show file tree
Hide file tree
Showing 9 changed files with 1,630 additions and 293 deletions.
1,839 changes: 1,553 additions & 286 deletions apidom/package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion apidom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
"link-parent-bin": "=1.0.2",
"lint-staged": "=10.2.7",
"microtime": "=3.0.0",
"mocha": "=7.2.0",
"mocha": "=8.4.0",
"mocha-chai-jest-snapshot": "=1.1.2",
"node-gyp": "=7.1.2",
"npm-run-all": "=4.1.5",
"prettier": "=2.0.5",
Expand Down
2 changes: 1 addition & 1 deletion apidom/packages/apidom-ns-asyncapi-2-0/.mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
module.exports = {
recursive: true,
spec: 'test/**/*.ts',
require: ['test/mocha-bootstrap.js'],
file: ['test/mocha-bootstrap.js'],
};
1 change: 1 addition & 0 deletions apidom/packages/apidom-ns-asyncapi-2-0/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"lint:fix": "eslint ./ --fix",
"clean": "rimraf ./es ./cjs ./dist ./types",
"test": "cross-env BABEL_ENV=cjs mocha",
"test:update-snapshots": "cross-env UPDATE_SNAPSHOT=1 BABEL_ENV=cjs mocha",
"perf": "cross-env BABEL_ENV=cjs node ./test/perf/index.js",
"perf:visitor-shortcut": "cross-env BABEL_ENV=cjs node ./test/perf/visitor-shortcut.js",
"typescript:check-types": "tsc --noEmit",
Expand Down
10 changes: 10 additions & 0 deletions apidom/packages/apidom-ns-asyncapi-2-0/test/mocha-bootstrap.js
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
require('@babel/register')({ extensions: ['.js', '.ts'], rootMode: 'upward' });

const chai = require('chai');
const { jestSnapshotPlugin, addSerializer } = require('mocha-chai-jest-snapshot');

const jestApiDOMSerializer = require('../../../scripts/jest-serializer-apidom');
const jestStringSerializer = require('../../../scripts/jest-serializer-string');

chai.use(jestSnapshotPlugin());
addSerializer(jestApiDOMSerializer);
addSerializer(jestStringSerializer);
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`refractor given generic ApiDOM object in AsyncApi 2.0 shape should refract to AsyncApi 2.0 Element 1`] = `
{
"element": "asyncApi2_0",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "api"
}
]
}
},
"content": [
{
"element": "member",
"content": {
"key": {
"element": "string",
"content": "asyncapi"
},
"value": {
"element": "asyncApiVersion",
"content": "2.0.0"
}
}
}
]
}
`;

exports[`refractor given generic ApiDOM object in AsyncApi 2.0 shape should refract to semantic ApiDOM tree 1`] = `
(AsyncApi2_0Element
(MemberElement
(StringElement)
(AsyncApiVersionElement)))
`;
15 changes: 10 additions & 5 deletions apidom/packages/apidom-ns-asyncapi-2-0/test/refractor/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { assert } from 'chai';
import { assert, expect } from 'chai';
import sinon from 'sinon';
import { Namespace } from 'minim';
import { ObjectElement, toValue } from 'apidom';
import { ObjectElement, toValue, sexprs } from 'apidom';

import * as predicates from '../../src/predicates';
import { AsyncApi2_0Element, AsyncApiVersionElement, isAsyncApiVersionElement } from '../../src';

describe('refractor', function () {
specify('should refract to AsyncApi 2.0 namespace', function () {
context('given generic ApiDOM object in AsyncApi 2.0 shape', function () {
const genericObject = new ObjectElement({
asyncapi: '2.0.0',
});
const asyncApiElement = AsyncApi2_0Element.refract(genericObject);

// console.log(toString(asyncApiElement));
assert.deepEqual(toValue(asyncApiElement), { asyncapi: '2.0.0' });
specify('should refract to semantic ApiDOM tree', function () {
expect(sexprs(asyncApiElement)).toMatchSnapshot();
});

specify('should refract to AsyncApi 2.0 Element', function () {
expect(asyncApiElement).toMatchSnapshot();
});
});

context('supports plugins', function () {
Expand Down
6 changes: 6 additions & 0 deletions apidom/scripts/jest-serializer-apidom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { isElement, dehydrate } = require('../packages/apidom');

module.exports = {
test: isElement,
print: (val) => JSON.stringify(dehydrate(val), null, 2),
};
7 changes: 7 additions & 0 deletions apidom/scripts/jest-serializer-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { identity } = require('ramda');
const { isString } = require('ramda-adjunct');

module.exports = {
test: isString,
print: identity,
};

0 comments on commit ac762f9

Please sign in to comment.