Skip to content

Commit

Permalink
chore(test): fix issue when running test with react major version env…
Browse files Browse the repository at this point in the history
… set

also reactor how we manipulate environment variables in our test
  • Loading branch information
100terres committed Aug 21, 2022
1 parent 7a6539d commit c663c72
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 20 deletions.
21 changes: 9 additions & 12 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// eslint-disable-next-line import/no-import-module-exports
import type { Config } from 'jest';
/* eslint-disable @typescript-eslint/triple-slash-reference */
/// <reference path="./test/typings/environment.d.ts" />

declare global {
interface ProcessEnv {
REACT_MAJOR_VERSION: string;
CI?: boolean;
}
}
import type { Config } from 'jest';
import getReactMajorVersion from './test/util/get-react-major-version';
import isRunningInCI from './test/util/is-running-in-ci';

const reactMajorVersion = process.env.REACT_MAJOR_VERSION;
const reactMajorVersion = getReactMajorVersion();

const config: Config = {
clearMocks: true,
Expand Down Expand Up @@ -39,7 +36,7 @@ const config: Config = {
};

// eslint-disable-next-line no-console
console.log('Testing with React version:', `${reactMajorVersion || '18'}.x.x`);
console.log('Testing with React version:', `${reactMajorVersion}.x.x`);

if (['16', '17'].includes(reactMajorVersion)) {
config.testPathIgnorePatterns = [
Expand All @@ -57,8 +54,8 @@ if (['16', '17'].includes(reactMajorVersion)) {
};
}

if (process.env.CI) {
if (isRunningInCI()) {
config.maxWorkers = 2;
}

module.exports = config;
export default config;
6 changes: 0 additions & 6 deletions test/setup/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import { TextDecoder, TextEncoder } from 'util';
import attachRafStub from './attach-raf-stub';
import transitionEventPolyfill from './transition-event-polyfill';

declare global {
interface ProcessEnv {
REACT_MAJOR_VERSION?: string;
}
}

export default class MyJSDOMEnvironment extends JSDOMEnvironment {
constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
super(config, context);
Expand Down
4 changes: 3 additions & 1 deletion test/setup/snapshot-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import getReactMajorVersion from '../util/get-react-major-version';

// resolve snapshot folder with the react version
// __react_16_snapshots__, __react_17_snapshots__, etc.
Expand All @@ -7,11 +8,12 @@ export default {
resolveSnapshotPath: (testPath: string, snapshotExtension: string) => {
const breadcrumb = testPath.split(path.sep);
const filename = breadcrumb.pop();
const reactMajorVersion = getReactMajorVersion();

return (
[
...breadcrumb,
`__react_${process.env.REACT_MAJOR_VERSION}_snapshots__`,
`__react_${reactMajorVersion}_snapshots__`,
filename,
].join(path.sep) + snapshotExtension
);
Expand Down
9 changes: 9 additions & 0 deletions test/typings/environment.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface ProcessEnv {
NODE_ENV?: 'development' | 'production';
REACT_MAJOR_VERSION?: '16' | '17' | '18';
CI?: boolean;
}

interface Process {
env: ProcessEnv;
}
3 changes: 2 additions & 1 deletion test/unit/state/registry/use-registry.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { DraggableId } from '../../../../src/types';
import { invariant } from '../../../../src/invariant';
import { getPreset } from '../../../util/dimension';
import { getDraggableEntry } from '../../../util/registry';
import getReactMajorVersion from '../../../util/get-react-major-version';
import useRegistry from '../../../../src/state/registry/use-registry';

const preset = getPreset();
Expand Down Expand Up @@ -35,7 +36,7 @@ it('should remove any registrations', () => {
unmount();

// only for react 16 and 17
if (['16', '17'].includes(`${process.env.REACT_MAJOR_VERSION}`)) {
if (['16', '17'].includes(getReactMajorVersion())) {
// still available after a unmount
// eslint-disable-next-line jest/no-conditional-expect
expect(registry.draggable.exists(id)).toBe(true);
Expand Down
3 changes: 3 additions & 0 deletions test/util/get-react-major-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function getReactMajorVersion(): '16' | '17' | '18' {
return process.env.REACT_MAJOR_VERSION || '18';
}
3 changes: 3 additions & 0 deletions test/util/is-running-in-ci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function isRunningInCI(): boolean {
return Boolean(process.env.CI);
}

0 comments on commit c663c72

Please sign in to comment.