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

Tech: make tests use SWC, not babel #20350

Closed
wants to merge 4 commits into from
Closed
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 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ node_modules
.idea
*.iml
*.sw*
!.swcrc
dist
*.DS_Store
.cache
Expand Down
1 change: 1 addition & 0 deletions code/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ node_modules
.idea
*.iml
*.sw*
!.swcrc
npm-shrinkwrap.json
dist
.tern-port
Expand Down
24 changes: 24 additions & 0 deletions code/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true
},
"transform": {
"react": {
"runtime": "classic",
"pragma": "React.createElement",
"pragmaFrag": "React.Fragment",
"throwIfNamespace": false,
"development": false
}
},
"target": "es2020",
"loose": false,
"externalHelpers": false,
// Requires v1.2.50 or upper and requires target to be es2016 or upper.
"keepClassNames": false
},
"minify": false
}
4 changes: 2 additions & 2 deletions code/addons/interactions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
},
"devDependencies": {
"@devtools-ds/object-inspector": "^1.1.2",
"@storybook/jest": "^0.0.10",
"@storybook/testing-library": "0.0.14-next.0",
"@storybook/jest": "next",
"@storybook/testing-library": "next",
"@types/node": "^16.0.0",
"formik": "^2.2.9",
"typescript": "~4.9.3"
Expand Down
16 changes: 1 addition & 15 deletions code/jest.config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,9 @@ module.exports = {
path.resolve('./__mocks__/fileMock.js'),
'\\.(css|scss|stylesheet)$': path.resolve('./__mocks__/styleMock.js'),
'\\.(md)$': path.resolve('./__mocks__/htmlMock.js'),

// core-js v2 to v3 mapping
'core-js/modules/es6.(.*)': 'core-js/modules/es.$1',
'core-js/modules/es7.(.*)': 'core-js/modules/esnext.$1',
'core-js/library/fn/(.*)': `core-js/features/$1`,
'core-js/es5/(.*)': `core-js/es/$1`,
'core-js/es6/(.*)': `core-js/es/$1`,
'core-js/es7/reflect': `core-js/proposals/reflect-metadata`,
'core-js/es7/(.*)': `core-js/proposals/$1`,
'core-js/object$/': `core-js/es/object`,
'core-js/object/(.*)': `core-js/es/object/$1`,
'babel-runtime/core-js/(.*)': `core-js/es/$1`,
// 'babel-runtime/core-js/object/assign'
'core-js/library/fn/object/assign': 'core-js/es/object/assign',
},
transform: {
'^.+\\.[jt]sx?$': path.resolve('../scripts/utils/jest-transform-js.js'),
'^.+\\.(t|j)sx?$': '@swc/jest',
'^.+\\.mdx$': '@storybook/addon-docs/jest-transform-mdx',
},
transformIgnorePatterns: ['/node_modules/(?!@angular|rxjs|nanoid|uuid|lit-html|@mdx-js)'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from '@jest/globals';
// import { describe, it, expect } from '@jest/globals';
import { dedent } from 'ts-dedent';
import _transform from '../csf-2-to-3';

Expand Down
16 changes: 6 additions & 10 deletions code/lib/manager-api/src/tests/stories.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/// <reference types="@types/jest" />;
// Need to import jest as mockJest for annoying jest reasons. Is there a better way?
import { jest, it, describe, expect, beforeEach } from '@jest/globals';

import {
STORY_ARGS_UPDATED,
Expand Down Expand Up @@ -34,7 +32,7 @@ function mockChannel() {
return new Channel({ transport });
}

const mockGetEntries = jest.fn<() => StoryIndex['entries']>();
const mockGetEntries = jest.fn();

jest.mock('../lib/events');
jest.mock('@storybook/global', () => ({
Expand Down Expand Up @@ -90,7 +88,7 @@ beforeEach(() => {
provider.serverChannel = mockChannel();
mockGetEntries.mockReset().mockReturnValue(mockEntries);

(global.fetch as ReturnType<typeof jest.fn<typeof global.fetch>>).mockReset().mockReturnValue(
(global.fetch as jest.Mock<ReturnType<typeof global.fetch>>).mockReset().mockReturnValue(
Promise.resolve({
status: 200,
ok: true,
Expand Down Expand Up @@ -567,7 +565,7 @@ describe('stories API', () => {
const store = createMockStore({});
const fullAPI = Object.assign(new EventEmitter(), {});

(global.fetch as ReturnType<typeof jest.fn<typeof global.fetch>>).mockReturnValue(
(global.fetch as jest.Mock<ReturnType<typeof global.fetch>>).mockReturnValue(
Promise.resolve({
status: 500,
text: async () => new Error('sorting error'),
Expand All @@ -593,13 +591,11 @@ describe('stories API', () => {
const { api, init } = initStories({ store, navigate, provider, fullAPI } as any);
Object.assign(fullAPI, api);

(global.fetch as ReturnType<typeof jest.fn<typeof global.fetch>>).mockClear();
(global.fetch as jest.Mock<ReturnType<typeof global.fetch>>).mockClear();
await init();
expect(global.fetch as ReturnType<typeof jest.fn<typeof global.fetch>>).toHaveBeenCalledTimes(
1
);
expect(global.fetch as jest.Mock<ReturnType<typeof global.fetch>>).toHaveBeenCalledTimes(1);

(global.fetch as ReturnType<typeof jest.fn<typeof global.fetch>>).mockClear();
(global.fetch as jest.Mock<ReturnType<typeof global.fetch>>).mockClear();
mockGetEntries.mockReturnValueOnce({
'component-a--story-1': {
type: 'story',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import React from 'react';
import { global } from '@storybook/global';
import type { RenderContext } from '@storybook/types';
import { expect } from '@jest/globals';
import { addons, mockChannel as createMockChannel } from '../addons';

import { PreviewWeb } from './PreviewWeb';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* @jest-environment jsdom
*/

import { jest, jest as mockJest, it, describe, beforeEach, afterEach, expect } from '@jest/globals';
import { global } from '@storybook/global';
import merge from 'lodash/merge';
import {
Expand Down Expand Up @@ -61,8 +60,8 @@ const mockStoryIndex = jest.fn(() => storyIndex);
let mockFetchResult: any;
jest.mock('@storybook/global', () => ({
global: {
...(mockJest.requireActual('@storybook/global') as any),
history: { replaceState: mockJest.fn() },
...(jest.requireActual('@storybook/global') as any),
history: { replaceState: jest.fn() },
document: {
location: {
pathname: 'pathname',
Expand Down Expand Up @@ -99,7 +98,6 @@ const createGate = (): [Promise<any | undefined>, (_?: any) => void] => {
// a timer, so we need to first setImmediate (to get past the resolution), then run the timers
// Probably jest modern timers do this but they aren't working for some bizarre reason.
async function waitForSetCurrentStory() {
// @ts-expect-error (Argument of type '{ doNotFake: string[]; }' is not assignable to parameter of type '"modern" | "legacy" | undefined'. ts(2345)))
jest.useFakeTimers({ doNotFake: ['setTimeout'] });
await new Promise((r) => setTimeout(r, 0));
jest.runAllTimers();
Expand Down Expand Up @@ -140,9 +138,7 @@ beforeEach(() => {
addons.setServerChannel(createMockChannel());
mockFetchResult = { status: 200, json: mockStoryIndex, text: () => 'error text' };

// @ts-expect-error (Property 'mocked' does not exist on type 'Jest'. Did you mean 'mock'? ts(2551))
jest.mocked(WebView.prototype).prepareForDocs.mockReturnValue('docs-element' as any);
// @ts-expect-error (Property 'mocked' does not exist on type 'Jest'. Did you mean 'mock'? ts(2551))
jest.mocked(WebView.prototype).prepareForStory.mockReturnValue('story-element' as any);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { jest, describe, it, expect } from '@jest/globals';
import { Channel } from '@storybook/channels';
import type { Renderer, StandaloneDocsIndexEntry } from '@storybook/types';
import type { StoryStore } from '../../store';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { jest, describe, it, expect } from '@jest/globals';
import { Channel } from '@storybook/channels';
import type { Renderer, StoryIndexEntry } from '@storybook/types';
import type { StoryStore } from '../../store';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { jest, describe, it, expect } from '@jest/globals';
import { Channel } from '@storybook/channels';
import type { Renderer, TemplateDocsIndexEntry } from '@storybook/types';
import type { StoryStore } from '../../store';
Expand Down
6 changes: 3 additions & 3 deletions code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
"@storybook/html-vite": "workspace:*",
"@storybook/html-webpack5": "workspace:*",
"@storybook/instrumenter": "workspace:*",
"@storybook/jest": "^0.0.10",
"@storybook/jest": "next",
"@storybook/linter-config": "^2.5.0",
"@storybook/manager": "workspace:*",
"@storybook/manager-api": "workspace:*",
Expand Down Expand Up @@ -210,7 +210,7 @@
"@storybook/svelte": "workspace:*",
"@storybook/svelte-webpack5": "workspace:*",
"@storybook/telemetry": "workspace:*",
"@storybook/testing-library": "0.0.14-next.0",
"@storybook/testing-library": "next",
"@storybook/theming": "workspace:*",
"@storybook/types": "workspace:*",
"@storybook/vue": "workspace:*",
Expand All @@ -222,6 +222,7 @@
"@storybook/web-components-vite": "workspace:*",
"@storybook/web-components-webpack5": "workspace:*",
"@swc/core": "^1.3.23",
"@swc/jest": "^0.2.24",
"@testing-library/dom": "^7.29.4",
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.2",
Expand All @@ -247,7 +248,6 @@
"@vitejs/plugin-react": "^2.1.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^29.3.1",
"babel-loader": "^8.3.0",
"chromatic": "6.11.2",
"codecov": "^3.8.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const shortcutKeys = {
search: ['/'],
focusNav: ['1'],
focusIframe: ['2'],
};
} as any;

const makeActions = () => ({
setShortcut: jest.fn(),
Expand Down Expand Up @@ -48,7 +48,9 @@ describe('ShortcutsScreen', () => {
const comp = shallow(<ShortcutsScreen shortcutKeys={shortcutKeys} {...makeActions()} />);
const instance = comp.instance();

// @ts-expect-error (please let's get rid of enzyme)
instance.onFocus('toolbar')();
// @ts-expect-error (please let's get rid of enzyme)
expect(comp.state('shortcutKeys').toolbar.shortcut).toBeNull();
expect(comp.state('activeFeature')).toBe('toolbar');
});
Expand All @@ -59,31 +61,42 @@ describe('ShortcutsScreen', () => {
const actions = makeActions();
const comp = shallow(<ShortcutsScreen shortcutKeys={shortcutKeys} {...actions} />);
const instance = comp.instance();
// @ts-expect-error (please let's get rid of enzyme)
instance.onFocus('focusIframe')();
// @ts-expect-error (please let's get rid of enzyme)
instance.onKeyDown({ isShift: true, key: 'Shift' });
expect(actions.setShortcut).not.toHaveBeenCalled();
// @ts-expect-error (please let's get rid of enzyme)
expect(comp.state('shortcutKeys').focusIframe.shortcut).toBeNull();
});

it('changes the shortcut in state if a key is pressed', () => {
const actions = makeActions();
const comp = shallow(<ShortcutsScreen shortcutKeys={shortcutKeys} {...actions} />);
const instance = comp.instance();
// @ts-expect-error (please let's get rid of enzyme)
instance.onFocus('focusIframe')();
// @ts-expect-error (please let's get rid of enzyme)
instance.onKeyDown({ key: 'P' });
expect(actions.setShortcut).not.toHaveBeenCalled();
// @ts-expect-error (please let's get rid of enzyme)
expect(comp.state('shortcutKeys').focusIframe.shortcut).toEqual(['P']);
// @ts-expect-error (please let's get rid of enzyme)
expect(comp.state('shortcutKeys').focusIframe.error).toBe(false);
});

it('sets an error and the shortcut in state if a duplicate key is pressed', () => {
const actions = makeActions();
const comp = shallow(<ShortcutsScreen shortcutKeys={shortcutKeys} {...actions} />);
const instance = comp.instance();
// @ts-expect-error (please let's get rid of enzyme)
instance.onFocus('focusIframe')();
// @ts-expect-error (please let's get rid of enzyme)
instance.onKeyDown({ key: 'F' });
expect(actions.setShortcut).not.toHaveBeenCalled();
// @ts-expect-error (please let's get rid of enzyme)
expect(comp.state('shortcutKeys').focusIframe.shortcut).toEqual(['F']);
// @ts-expect-error (please let's get rid of enzyme)
expect(comp.state('shortcutKeys').focusIframe.error).toBe(true);
});
});
Expand All @@ -93,39 +106,53 @@ describe('ShortcutsScreen', () => {
const actions = makeActions();
const comp = shallow(<ShortcutsScreen shortcutKeys={shortcutKeys} {...actions} />);
const instance = comp.instance();
// @ts-expect-error (please let's get rid of enzyme)
instance.onFocus('focusIframe')();
// @ts-expect-error (please let's get rid of enzyme)
await instance.onBlur();

expect(actions.setShortcut).not.toHaveBeenCalled();
expect(actions.restoreDefaultShortcut).toHaveBeenCalledWith('focusIframe');
// @ts-expect-error (please let's get rid of enzyme)
expect(comp.state('shortcutKeys').focusIframe.shortcut).toEqual(['2']);
// @ts-expect-error (please let's get rid of enzyme)
expect(comp.state('shortcutKeys').focusIframe.error).toBe(false);
});

it('if the shortcut is errored, restores the respective default', async () => {
const actions = makeActions();
const comp = shallow(<ShortcutsScreen shortcutKeys={shortcutKeys} {...actions} />);
const instance = comp.instance();
// @ts-expect-error (please let's get rid of enzyme)
instance.onFocus('focusIframe')();
// @ts-expect-error (please let's get rid of enzyme)
instance.onKeyDown({ key: 'F' });
// @ts-expect-error (please let's get rid of enzyme)
await instance.onBlur();

expect(actions.setShortcut).not.toHaveBeenCalled();
expect(actions.restoreDefaultShortcut).toHaveBeenCalledWith('focusIframe');
// @ts-expect-error (please let's get rid of enzyme)
expect(comp.state('shortcutKeys').focusIframe.shortcut).toEqual(['2']);
// @ts-expect-error (please let's get rid of enzyme)
expect(comp.state('shortcutKeys').focusIframe.error).toBe(false);
});

it('saves the shortcut if it is valid', () => {
const actions = makeActions();
const comp = shallow(<ShortcutsScreen shortcutKeys={shortcutKeys} {...actions} />);
const instance = comp.instance();
// @ts-expect-error (please let's get rid of enzyme)
instance.onFocus('focusIframe')();
// @ts-expect-error (please let's get rid of enzyme)
instance.onKeyDown({ key: 'P' });
// @ts-expect-error (please let's get rid of enzyme)
instance.onBlur();

expect(actions.setShortcut).toHaveBeenCalledWith('focusIframe', ['P']);
// @ts-expect-error (please let's get rid of enzyme)
expect(comp.state('shortcutKeys').focusIframe.shortcut).toEqual(['P']);
// @ts-expect-error (please let's get rid of enzyme)
expect(comp.state('shortcutKeys').focusIframe.error).toBe(false);
});
});
Expand All @@ -135,10 +162,13 @@ describe('ShortcutsScreen', () => {
const actions = makeActions();
const comp = shallow(<ShortcutsScreen shortcutKeys={shortcutKeys} {...actions} />);
const instance = comp.instance();
// @ts-expect-error (please let's get rid of enzyme)
instance.onFocus('focusIframe')();
// @ts-expect-error (please let's get rid of enzyme)
instance.onKeyDown({ key: 'P' });

await comp.find('#restoreDefaultsHotkeys').simulate('click');
// @ts-expect-error (please let's get rid of enzyme)
expect(comp.state('shortcutKeys').focusIframe.shortcut).toEqual(['2']);
});
});
Expand Down
Loading