Skip to content

Commit

Permalink
chore: Convert LSP tests to jest from mocha (graphql#871)
Browse files Browse the repository at this point in the history
- added fix and comment for new failing tests
- added assertions for new tests for Jest
- switched back to global jest run, 12.8s for all but codemirror! woo!
- make sure mocks arent being built by babel as well (fixes warning)
  • Loading branch information
acao authored Sep 8, 2019
1 parent 2210b9d commit 7762c3d
Show file tree
Hide file tree
Showing 41 changed files with 598 additions and 468 deletions.
24 changes: 24 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
codecov:
notify:
require_ci_to_pass: no

coverage:
precision: 2
round: down
range: "30...100"

status:
project: yes
patch: yes
changes: no

flags:
graphiql:
paths:
- packages/graphiql/
graphql-language-service-interface:
paths:
- packages/graphql-language-service-interface/
graphql-language-service-utils:
paths:
- packages/graphql-language-service-utils/
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ before_install:
- npm config set spin false --global
install:
- yarn --ignore-scripts # ignore prepublish scripts
after_success:
- codecov
cache: yarn
# deploy:
# - provider: script
Expand Down
28 changes: 28 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const path = require('path');

module.exports = {
verbose: true,
clearMocks: true,
collectCoverage: true,
setupFiles: [path.join(__dirname, '/resources/enzyme.config.js')],
testMatch: [
'<rootDir>/packages/*/src/**/*-test.js',
'<rootDir>/packages/*/src/**/*.spec.js',
],
transform: {
'^.+\\.jsx?$': require.resolve('./resources/jestBabelTransform'),
},
testEnvironment: require.resolve('jest-environment-jsdom-global'),
testPathIgnorePatterns: [
'node_modules',
'dist',
'codemirror-graphql',
],
collectCoverageFrom: [
'**/src/**/*.{js,jsx}',
'!**/node_modules/**',
'!**/__tests__/**',
'!**/resources/**',
'!packages/codemirrir-graphql/**',
],
};
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
"packages/*",
"packages/graphiql-examples/*"
],
"options": {
"mocha": "--full-trace --require @babel/register --require resources/mochaBootload 'packages/{*,!graphiql}/src/**/__tests__/**/*-test.js'",
"mocha_tdd": "--full-trace --watch --require resources/mochaBootload packages/*/src/**/__tests__/**/*-test.js"
},
"browserify-shim": {
"react": "global:React",
"react-dom": "global:ReactDOM"
Expand All @@ -25,7 +21,7 @@
"scripts": {
"build": "lerna run build",
"test": "yarn run lint && yarn run check && yarn run build && yarn run testonly",
"testonly": "mocha $npm_package_options_mocha && lerna run test --scope graphiql",
"testonly": "jest && lerna run test --scope codemirror-graphql",
"t": "yarn run testonly",
"lint": "eslint packages/**/src || (printf '\\033[33mTry: \\033[7m yarn run lint -- --fix \\033[0m\\n' && exit 1)",
"lint-check": "eslint --print-config .eslintrc.js | eslint-config-prettier-check",
Expand Down Expand Up @@ -54,6 +50,7 @@
"babel-eslint": "^10.0.1",
"chai": "4.2.0",
"conventional-changelog-conventionalcommits": "^4.1.0",
"codecov": "^3.5.0",
"eslint": "^5.16.0",
"eslint-config-prettier": "4.3.0",
"eslint-plugin-babel": "5.3.0",
Expand All @@ -67,6 +64,9 @@
"husky": "^3.0.5",
"lerna": "^3.16.4",
"mocha": "6.1.4",
"jest": "^24.8.0",
"jest-environment-jsdom": "^24.8.0",
"jest-environment-jsdom-global": "^1.2.0",
"prettier": "^1.18.2"
}
}
2 changes: 1 addition & 1 deletion packages/codemirror-graphql/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
node_modules/
coverage/
/*.js
!.babelrc.js
!babel.config.js
/utils
/variables
/results
Expand Down
8 changes: 8 additions & 0 deletions packages/codemirror-graphql/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
presets: [
require.resolve('@babel/preset-env'),
require.resolve('@babel/preset-flow'),
require.resolve('@babel/preset-react'),
],
plugins: [require.resolve('@babel/plugin-proposal-class-properties')],
};
9 changes: 8 additions & 1 deletion packages/codemirror-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@
"README.md",
"LICENSE"
],
"options": {
"mocha": "--full-trace --require resources/mochaBootload src/*/__tests__/*-test.js",
"mocha_tdd": "--full-trace --watch --require resources/mochaBootload src/**/*/__tests__/*-test.js"
},
"scripts": {
"lint": "eslint src",
"check": "flow check",
"build": "babel src --root-mode upward --ignore __tests__ --out-dir .",
"build-js": "node ../../resources/buildJs.js",
"build-flow": "node ../../resources/buildFlow.js",
"watch": "babel --optional runtime resources/watch.js | node"
"watch": "babel --optional runtime resources/watch.js | node",
"preversion": ". ./resources/checkgit.sh && yarn test",
"prepublish": ". ./resources/prepublish.sh",
"test": "mocha $npm_package_options_mocha"
},
"peerDependencies": {
"codemirror": "^5.26.0",
Expand Down
63 changes: 63 additions & 0 deletions packages/codemirror-graphql/resources/bootJSDOM.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function bootJSDOM() {
/* eslint-disable no-console, object-shorthand */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
const doc = jsdom;

// get the window object out of the document
const win = doc.window;

// set globals for mocha that make access to document and window feel
// natural in the test environment
global.document = win.document;
global.window = win;

global.document.createRange = function() {
return {
setEnd: function() {},
setStart: function() {},
getClientRects: function() {
return { top: 0, bottom: 0, left: 0, right: 0 };
},
getBoundingClientRect: function() {
return { right: 0 };
},
};
};

// take all properties of the window object and also attach it to the
// mocha global object
propagateToGlobal(win);

// from mocha-jsdom
// https://github.com/rstacruz/mocha-jsdom/blob/master/index.js#L80
function propagateToGlobal(window) {
for (const key in window) {
if (!window.hasOwnProperty(key)) {
continue;
}
if (key in global) {
continue;
}
global[key] = window[key];
}
}

const chai = require('chai');

const chaiSubset = require('chai-subset');
chai.use(chaiSubset);

process.on('unhandledRejection', (error) => {
console.error('Unhandled Promise Rejection:');
console.error((error && error.stack) || error);
});
}

module.exports = bootJSDOM
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
require('@babel/register', {
rootMode: 'upward',
});

require('@babel/polyfill');
require('@babel/polyfill', {
rootMode: 'upward',
});

const JSDOM = require('jsdom').JSDOM;

Expand Down
19 changes: 2 additions & 17 deletions packages/graphiql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,14 @@
"README.md",
"LICENSE"
],
"jest": {
"clearMocks": true,
"setupFiles": [
"<rootDir>/resources/enzyme.config.js"
],
"transform": {
"^.+\\.jsx?$": "./resources/jestBabelTransform"
},
"testEnvironment": "jsdom",
"testPathIgnorePatterns": [
"\\\\node_modules\\\\",
"dist"
]
},
"scripts": {
"build": "yarn run build-js && yarn run build-flow",
"build-js": "bash ./resources/build.sh",
"build-flow": "node ../../resources/buildFlow.js",
"check": "flow check",
"dev": "babel-node --root-mode upward test/server.js",
"test": "jest --no-cache spec"
"preversion": ". ./resources/checkgit.sh && npm test",
"test": "node ../../resources/runTests ENZYME=true"
},
"lint-staged": {
"*.js": [
Expand All @@ -67,8 +54,6 @@
"babelify": "10.0.0",
"browserify": "16.2.3",
"browserify-shim": "3.8.14",
"chai": "4.2.0",
"chai-subset": "1.6.0",
"enzyme": "^3.9.0",
"enzyme-adapter-react-15": "^1.4.0",
"express": "5.0.0-alpha.5",
Expand Down
26 changes: 0 additions & 26 deletions packages/graphiql/resources/enzyme.config.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import React from 'react';
import { mount } from 'enzyme';
import { expect } from 'chai';

import FieldDoc from '../FieldDoc';

Expand All @@ -34,17 +33,17 @@ const exampleObject = new GraphQLObjectType({
},
});

describe.only('FieldDoc', () => {
describe('FieldDoc', () => {
it('should render a simple string field', () => {
const W = mount(
<FieldDoc
field={exampleObject.getFields().string}
onClickType={jest.fn()}
/>,
);
expect(W.find('MarkdownContent').text()).to.equal('No Description\n');
expect(W.find('TypeLink').text()).to.equal('String');
expect(W.find('Argument').length).to.equal(0);
expect(W.find('MarkdownContent').text()).toEqual('No Description\n');
expect(W.find('TypeLink').text()).toEqual('String');
expect(W.find('Argument').length).toEqual(0);
});

it('should re-render on field change', () => {
Expand All @@ -54,9 +53,9 @@ describe.only('FieldDoc', () => {
onClickType={jest.fn()}
/>,
);
expect(W.find('MarkdownContent').text()).to.equal('No Description\n');
expect(W.find('TypeLink').text()).to.equal('String');
expect(W.find('Argument').length).to.equal(0);
expect(W.find('MarkdownContent').text()).toEqual('No Description\n');
expect(W.find('TypeLink').text()).toEqual('String');
expect(W.find('Argument').length).toEqual(0);
});

it('should render a string field with arguments', () => {
Expand All @@ -70,13 +69,13 @@ describe.only('FieldDoc', () => {
W.find('TypeLink')
.at(0)
.text(),
).to.equal('String');
).toEqual('String');
expect(
W.find('.doc-type-description')
.at(0)
.text(),
).to.equal('Example String field with arguments\n');
expect(W.find('Argument').length).to.equal(1);
expect(W.find('Argument').text()).to.equal('stringArg: String');
).toEqual('Example String field with arguments\n');
expect(W.find('Argument').length).toEqual(1);
expect(W.find('Argument').text()).toEqual('stringArg: String');
});
});
Loading

0 comments on commit 7762c3d

Please sign in to comment.