[refactor] Migrate from Mocha+Chai to Jest (#6079)

* [refactor] Migrate from Mocha+Chai to Jest

This change migrates all the existing unit tests
- to Jest's global expect and matchers from chai's imported expect, asserts and matchers.
- to Jest's describe/test from mocha's describe/it

The majority of the mechanical changes to tests are achieved through running jest-codemods. The only two note-worthy manual tweaks:
1. Setting a testURL of http://localhost in jest config and adjusting a few tests to leverage this value instead of relying on about:blank.
2. Re-enabling ExploreChartPanel_spec which was previously commented out as we cannot have empty tests with nothing in it with Jest. :)

This change also removes dependencies to Mocha and Chai.

* Remove the test:one command as it now does the same thing as test.

* Fixing lint errors. The diff looks large but is large done through `yarn run lint --fix`

The only noteworthy change is the one in eslintrc for tests. The env has been updated from mocha to jest.

* Adding eslint-plugin-jest and further modify tests.

- One small fix in sqllab's Timer Spec for a test that is not using the spy it created for testing.
- Deletion of a duplicated test caught by eslint-plugin-jest.

* - Make istanbul coverage work with Jest.

- Remove dependency on stand-alone istanbul and babel-istanbul as they're built-into jest. Yes!

* Attempt to fix dynamic imports in tests.

* run sequentially and log heap usage

* - tweaking maxworkers for travis and specifying coverageDirectory for codecov

- remove dynamic import in shim.js now that it is set in babelrc for tests only.
This commit is contained in:
Christine Chambers
2018-10-15 13:10:18 -07:00
committed by Chris Williams
parent 46c86672c8
commit 9029701f24
177 changed files with 2539 additions and 2166 deletions

View File

@@ -1,5 +1,3 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';
import ChartPlugin from '../../../../src/visualizations/core/models/ChartPlugin';
import ChartMetadata from '../../../../src/visualizations/core/models/ChartMetadata';
@@ -7,7 +5,7 @@ describe('ChartPlugin', () => {
const metadata = new ChartMetadata({});
it('exists', () => {
expect(ChartPlugin).to.not.equal(undefined);
expect(ChartPlugin).toBeDefined();
});
describe('new ChartPlugin()', () => {
@@ -16,13 +14,13 @@ describe('ChartPlugin', () => {
metadata,
Chart() {},
});
expect(plugin).to.be.instanceof(ChartPlugin);
expect(plugin).toBeInstanceOf(ChartPlugin);
});
it('throws an error if metadata is not specified', () => {
expect(() => new ChartPlugin()).to.throw(Error);
expect(() => new ChartPlugin()).toThrowError(Error);
});
it('throws an error if none of Chart or loadChart is specified', () => {
expect(() => new ChartPlugin({ metadata })).to.throw(Error);
expect(() => new ChartPlugin({ metadata })).toThrowError(Error);
});
});
@@ -32,11 +30,11 @@ describe('ChartPlugin', () => {
Chart() {},
});
it('throws an error if key is not provided', () => {
expect(() => plugin.register()).to.throw(Error);
expect(() => plugin.configure({ key: 'abc' }).register()).to.not.throw(Error);
expect(() => plugin.register()).toThrowError(Error);
expect(() => plugin.configure({ key: 'abc' }).register()).not.toThrowError(Error);
});
it('returns itself', () => {
expect(plugin.configure({ key: 'abc' }).register()).to.equal(plugin);
expect(plugin.configure({ key: 'abc' }).register()).toBe(plugin);
});
});
});

View File

@@ -1,35 +1,39 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';
import Plugin from '../../../../src/visualizations/core/models/Plugin';
describe('Plugin', () => {
it('exists', () => {
expect(Plugin).to.not.equal(undefined);
expect(Plugin).toBeDefined();
});
describe('new Plugin()', () => {
it('creates a new plugin', () => {
const plugin = new Plugin();
expect(plugin).to.be.instanceof(Plugin);
expect(plugin).toBeInstanceOf(Plugin);
});
});
describe('.configure(config, replace)', () => {
it('extends the default config with given config when replace is not set or false', () => {
const plugin = new Plugin();
plugin.configure({ key: 'abc', foo: 'bar' });
plugin.configure({ key: 'def' });
expect(plugin.config).to.deep.equal({ key: 'def', foo: 'bar' });
});
it('replaces the default config with given config when replace is true', () => {
const plugin = new Plugin();
plugin.configure({ key: 'abc', foo: 'bar' });
plugin.configure({ key: 'def' }, true);
expect(plugin.config).to.deep.equal({ key: 'def' });
});
it(
'extends the default config with given config when replace is not set or false',
() => {
const plugin = new Plugin();
plugin.configure({ key: 'abc', foo: 'bar' });
plugin.configure({ key: 'def' });
expect(plugin.config).toEqual({ key: 'def', foo: 'bar' });
},
);
it(
'replaces the default config with given config when replace is true',
() => {
const plugin = new Plugin();
plugin.configure({ key: 'abc', foo: 'bar' });
plugin.configure({ key: 'def' }, true);
expect(plugin.config).toEqual({ key: 'def' });
},
);
it('returns the plugin itself', () => {
const plugin = new Plugin();
expect(plugin.configure({ key: 'abc' })).to.equal(plugin);
expect(plugin.configure({ key: 'abc' })).toBe(plugin);
});
});
@@ -38,11 +42,11 @@ describe('Plugin', () => {
const plugin = new Plugin();
plugin.configure({ key: 'abc', foo: 'bar' });
plugin.resetConfig();
expect(plugin.config).to.deep.equal({});
expect(plugin.config).toEqual({});
});
it('returns the plugin itself', () => {
const plugin = new Plugin();
expect(plugin.resetConfig()).to.equal(plugin);
expect(plugin.resetConfig()).toBe(plugin);
});
});
});

View File

@@ -1,17 +1,15 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';
import Preset from '../../../../src/visualizations/core/models/Preset';
import Plugin from '../../../../src/visualizations/core/models/Plugin';
describe('Preset', () => {
it('exists', () => {
expect(Preset).to.not.equal(undefined);
expect(Preset).toBeDefined();
});
describe('new Preset()', () => {
it('creates new preset', () => {
const preset = new Preset();
expect(preset).to.be.instanceOf(Preset);
expect(preset).toBeInstanceOf(Preset);
});
});
@@ -54,12 +52,12 @@ describe('Preset', () => {
],
});
preset3.register();
expect(values).to.deep.equal([1, 2, 3, 'abc']);
expect(values).toEqual([1, 2, 3, 'abc']);
});
it('returns itself', () => {
const preset = new Preset();
expect(preset.register()).to.equal(preset);
expect(preset.register()).toBe(preset);
});
});
});

View File

@@ -1,5 +1,3 @@
import { expect } from 'chai';
import { formatLabel, tryNumify } from '../../../../src/visualizations/nvd3/utils';
describe('nvd3/utils', () => {
@@ -9,27 +7,27 @@ describe('nvd3/utils', () => {
};
describe('formatLabel()', () => {
it('formats simple labels', () => {
expect(formatLabel('foo')).to.equal('foo');
expect(formatLabel(['foo'])).to.equal('foo');
expect(formatLabel(['foo', 'bar'])).to.equal('foo, bar');
expect(formatLabel('foo')).toBe('foo');
expect(formatLabel(['foo'])).toBe('foo');
expect(formatLabel(['foo', 'bar'])).toBe('foo, bar');
});
it('formats simple labels with lookups', () => {
expect(formatLabel('foo', verboseMap)).to.equal('Foo');
expect(formatLabel('baz', verboseMap)).to.equal('baz');
expect(formatLabel(['foo'], verboseMap)).to.equal('Foo');
expect(formatLabel(['foo', 'bar', 'baz'], verboseMap)).to.equal('Foo, Bar, baz');
expect(formatLabel('foo', verboseMap)).toBe('Foo');
expect(formatLabel('baz', verboseMap)).toBe('baz');
expect(formatLabel(['foo'], verboseMap)).toBe('Foo');
expect(formatLabel(['foo', 'bar', 'baz'], verboseMap)).toBe('Foo, Bar, baz');
});
it('deals with time shift properly', () => {
expect(formatLabel(['foo', '1 hour offset'], verboseMap)).to.equal('Foo, 1 hour offset');
expect(formatLabel(['foo', 'bar', 'baz', '2 hours offset'], verboseMap)).to.equal('Foo, Bar, baz, 2 hours offset');
expect(formatLabel(['foo', '1 hour offset'], verboseMap)).toBe('Foo, 1 hour offset');
expect(formatLabel(['foo', 'bar', 'baz', '2 hours offset'], verboseMap)).toBe('Foo, Bar, baz, 2 hours offset');
});
});
describe('tryNumify()', () => {
it('tryNumify works as expected', () => {
expect(tryNumify(5)).to.equal(5);
expect(tryNumify('5')).to.equal(5);
expect(tryNumify('5.1')).to.equal(5.1);
expect(tryNumify('a string')).to.equal('a string');
expect(tryNumify(5)).toBe(5);
expect(tryNumify('5')).toBe(5);
expect(tryNumify('5.1')).toBe(5.1);
expect(tryNumify('a string')).toBe('a string');
});
});
});

View File

@@ -1,4 +1,3 @@
import { expect } from 'chai';
import $ from 'jquery';
import '../../helpers/shim';
import tableVis from '../../../src/visualizations/Table/adaptor';
@@ -33,24 +32,24 @@ describe('table viz', () => {
it('renders into a container', () => {
$('body').html(div);
const container = $(baseSlice.selector);
expect(container.length).to.equal(1);
expect(container).toHaveLength(1);
});
it('renders header and body datatables in container', () => {
$('body').html(div);
const container = $(baseSlice.selector);
expect(container.find('.dataTable').length).to.equal(0);
expect(container.find('.dataTable')).toHaveLength(0);
tableVis(baseSlice, basePayload);
expect(container.find('.dataTable').length).to.equal(2);
expect(container.find('.dataTable')).toHaveLength(2);
const tableHeader = container.find('.dataTable')[0];
expect($(tableHeader).find('thead tr').length).to.equal(1);
expect($(tableHeader).find('th').length).to.equal(2);
expect($(tableHeader).find('thead tr')).toHaveLength(1);
expect($(tableHeader).find('th')).toHaveLength(2);
const tableBody = container.find('.dataTable')[1];
expect($(tableBody).find('tbody tr').length).to.equal(2);
expect($(tableBody).find('th').length).to.equal(2);
expect($(tableBody).find('tbody tr')).toHaveLength(2);
expect($(tableBody).find('th')).toHaveLength(2);
});
it('hides the sort by column', () => {
@@ -74,7 +73,7 @@ describe('table viz', () => {
const container = $(slice.selector);
const tableHeader = container.find('.dataTable')[0];
expect($(tableHeader).find('th').length).to.equal(2);
expect($(tableHeader).find('th')).toHaveLength(2);
});
it('works with empty list for sort by', () => {
@@ -96,6 +95,6 @@ describe('table viz', () => {
const container = $(slice.selector);
const tableBody = container.find('.dataTable')[1];
expect($(tableBody).find('th').length).to.equal(3);
expect($(tableBody).find('th')).toHaveLength(3);
});
});