Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

chore: coordinate superset-ui unittest with main repository #1463

Merged
merged 1 commit into from
Nov 10, 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
5 changes: 0 additions & 5 deletions .esprintrc

This file was deleted.

23 changes: 8 additions & 15 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,16 @@ module.exports = {
},
moduleFileExtensions: ['mock.js', 'ts', 'tsx', 'js', 'jsx', 'json', 'node'],
moduleNameMapper: {
'^.+\\.(ttf|eot|otf|svg|woff|woff2|mp3|png|jpg|jpeg|gif|ico)$':
'<rootDir>/node_modules/@airbnb/config-jest/mocks/file.js',
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/__mocks__/fileMock.js',
'\\.(css|less)$': 'identity-obj-proxy',
'\\.(css|less|geojson)$': '<rootDir>/test/__mocks__/mockExportObject.js',
'\\.(gif|ttf|eot|png|jpg)$': '<rootDir>/test/__mocks__/mockExportString.js',
'\\.svg$': '<rootDir>/test/__mocks__/svgrMock.tsx',
'@superset-ui/(((?!(legacy-preset-chart-deckgl|core/src)).)*)$':
'<rootDir>/node_modules/@superset-ui/$1/src',
'@superset-ui/core/src/(.*)$':
'<rootDir>/node_modules/@superset-ui/core/src/$1',
},
roots: ['<rootDir>/packages', '<rootDir>/plugins'],
setupFiles: [
'<rootDir>/node_modules/@airbnb/config-jest/setup/shims.js',
'<rootDir>/node_modules/@airbnb/config-jest/setup/console.js',
'<rootDir>/node_modules/@airbnb/config-jest/setup/dom.js',
],
setupFilesAfterEnv: [
'<rootDir>/node_modules/@airbnb/config-jest/bootstrap/react.js',
'<rootDir>/node_modules/@airbnb/config-jest/bootstrap/consumer.js',
'@airbnb/config-jest/enzyme',
],
setupFiles: ['<rootDir>/test/setup.ts'],
testEnvironment: 'jsdom',
testURL: 'http://localhost',
timers: 'real',
Expand Down
13 changes: 13 additions & 0 deletions packages/superset-ui-core/test/translation/Translator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

import { logging } from '@superset-ui/core';
import Translator from '@superset-ui/core/src/translation/Translator';
import {
configure,
Expand All @@ -34,6 +35,18 @@ configure({
});

describe('Translator', () => {
const spy = jest.spyOn(logging, 'warn');

beforeAll(() => {
spy.mockImplementation((info: any) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why do we need to mock logging methods? And why do they throw errors?

Copy link
Contributor Author

@zhaoyongjie zhaoyongjie Nov 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed @airbnb/config-jest/setup/console.js from jest.config.js. this script makes console.warn return a mock function.(This logic is simple though. But it stuck me for a long time.)

So I'm thinking of using the normal mock function in the unit test is better than before.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, so we just bring back relevant changes from removed library 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some test cases that need to get warning message.

throw new Error(info);
});
});

afterAll(() => {
spy.mockRestore();
});

describe('new Translator(config)', () => {
it('initializes when config is not specified', () => {
expect(new Translator()).toBeInstanceOf(Translator);
Expand Down
11 changes: 11 additions & 0 deletions packages/superset-ui-core/test/utils/logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
describe('logging', () => {
beforeEach(() => {
jest.resetModules();
// Explicit is better than implicit
console.warn = console.error = function mockedConsole(message) {
throw new Error(message);
};
});
it('should pipe to `console` methods', () => {
const { logging } = require('@superset-ui/core/src');
Expand All @@ -36,9 +40,16 @@ describe('logging', () => {
expect(() => {
logging.error('error');
}).toThrow('error');

// to support: npx jest --silent
const spy = jest.spyOn(logging, 'trace');
spy.mockImplementation(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we include logging.trace's argument here? Like:
throw new Error('Trace: ' + traceMessage)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mock just for logging.trace(), Ensure that when call the logging.trace(), there is Trace: at the beginning of the return.

throw new Error('Trace:');
});
expect(() => {
logging.trace();
}).toThrow('Trace:');
spy.mockRestore();
});
it('should use noop functions when console unavailable', () => {
const { console } = window;
Expand Down
19 changes: 19 additions & 0 deletions test/__mocks__/mockExportObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
module.exports = {};
19 changes: 19 additions & 0 deletions test/__mocks__/mockExportString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
module.exports = 'test-file-stub';
29 changes: 29 additions & 0 deletions test/__mocks__/svgrMock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import React, { SVGProps } from 'react';

const SvgrMock = React.forwardRef<SVGSVGElement, SVGProps<SVGSVGElement>>(
(props, ref) => <svg ref={ref} {...props} />,
);

SvgrMock.displayName = 'SvgrMock';

export const ReactComponent = SvgrMock;
export default SvgrMock;
7 changes: 4 additions & 3 deletions test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
* specific language governing permissions and limitations
* under the License.
*/
import { configure } from '@superset-ui/core';
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import CacheStorage from './shims/CacheStorage';

configure();

// @ts-ignore
global.caches = new CacheStorage();

configure({ adapter: new Adapter() });