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

test: add support for doing snapshot testing #433

Merged
merged 1 commit into from
Jun 2, 2021
Merged
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,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,
};