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

This commit is contained in:
Yongjie Zhao 2021-11-10 16:11:01 +00:00
parent 490029a1f3
commit 067fc8a009
8 changed files with 103 additions and 23 deletions

View File

@ -1,5 +0,0 @@
{
"paths": ["{packages,plugins}/*/{src,test,types}/**/*.{ts,tsx,js,jsx}"],
"ignores": ["**/node_modules/**/*"],
"port": 5004
}

View File

@ -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',

View File

@ -17,6 +17,7 @@
* under the License.
*/
import { logging } from '@superset-ui/core';
import Translator from '@superset-ui/core/src/translation/Translator';
import {
configure,
@ -34,6 +35,18 @@ configure({
});
describe('Translator', () => {
const spy = jest.spyOn(logging, 'warn');
beforeAll(() => {
spy.mockImplementation((info: any) => {
throw new Error(info);
});
});
afterAll(() => {
spy.mockRestore();
});
describe('new Translator(config)', () => {
it('initializes when config is not specified', () => {
expect(new Translator()).toBeInstanceOf(Translator);

View File

@ -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');
@ -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(() => {
throw new Error('Trace:');
});
expect(() => {
logging.trace();
}).toThrow('Trace:');
spy.mockRestore();
});
it('should use noop functions when console unavailable', () => {
const { console } = window;

View File

@ -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 = {};

View File

@ -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';

View File

@ -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;

View File

@ -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() });