@mui/internal-test-utils 1.0.18 → 1.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/build/.tsbuildinfo +1 -1
  2. package/build/chai.types.d.ts +75 -0
  3. package/build/chai.types.d.ts.map +1 -0
  4. package/build/chai.types.js +3 -0
  5. package/build/chai.types.js.map +1 -0
  6. package/build/chaiPlugin.d.ts +5 -0
  7. package/build/chaiPlugin.d.ts.map +1 -0
  8. package/build/chaiPlugin.js +406 -0
  9. package/build/chaiPlugin.js.map +1 -0
  10. package/build/createDOM.js +4 -4
  11. package/build/createDOM.js.map +1 -1
  12. package/build/createRenderer.d.ts +37 -62
  13. package/build/createRenderer.d.ts.map +1 -1
  14. package/build/createRenderer.js +85 -40
  15. package/build/createRenderer.js.map +1 -1
  16. package/build/describeConformance.d.ts +4 -0
  17. package/build/describeConformance.d.ts.map +1 -1
  18. package/build/describeConformance.js +51 -14
  19. package/build/describeConformance.js.map +1 -1
  20. package/build/describeSkipIf.d.ts +3 -0
  21. package/build/describeSkipIf.d.ts.map +1 -0
  22. package/build/describeSkipIf.js +10 -0
  23. package/build/describeSkipIf.js.map +1 -0
  24. package/build/index.d.ts +1 -0
  25. package/build/index.d.ts.map +1 -1
  26. package/build/index.js +3 -1
  27. package/build/index.js.map +1 -1
  28. package/build/initMatchers.d.ts +1 -74
  29. package/build/initMatchers.d.ts.map +1 -1
  30. package/build/initMatchers.js +4 -400
  31. package/build/initMatchers.js.map +1 -1
  32. package/build/initMatchers.test.js +4 -6
  33. package/build/initMatchers.test.js.map +1 -1
  34. package/build/mochaHooks.test.js +5 -1
  35. package/build/mochaHooks.test.js.map +1 -1
  36. package/build/setupJSDOM.js +2 -2
  37. package/build/setupJSDOM.js.map +1 -1
  38. package/build/setupVitest.d.ts +2 -0
  39. package/build/setupVitest.d.ts.map +1 -0
  40. package/build/setupVitest.js +121 -0
  41. package/build/setupVitest.js.map +1 -0
  42. package/package.json +12 -10
  43. package/src/chai.types.ts +106 -0
  44. package/src/chaiPlugin.ts +512 -0
  45. package/src/createDOM.js +4 -4
  46. package/src/createRenderer.tsx +106 -51
  47. package/src/describeConformance.tsx +67 -16
  48. package/src/describeSkipIf.tsx +9 -0
  49. package/src/index.ts +1 -0
  50. package/src/initMatchers.test.js +4 -6
  51. package/src/initMatchers.ts +4 -615
  52. package/src/mochaHooks.test.js +2 -1
  53. package/src/setupJSDOM.js +2 -2
  54. package/src/setupVitest.ts +117 -0
  55. package/tsconfig.json +5 -2
@@ -1,23 +1,22 @@
1
1
  /* eslint-env mocha */
2
- import * as React from 'react';
3
- import * as ReactDOMServer from 'react-dom/server';
4
2
  import createEmotionCache from '@emotion/cache';
5
3
  import { CacheProvider as EmotionCacheProvider } from '@emotion/react';
6
4
  import {
7
- act as rtlAct,
8
5
  buildQueries,
9
6
  cleanup,
10
- fireEvent as rtlFireEvent,
11
- queries,
12
- queryHelpers,
13
- render as testingLibraryRender,
14
7
  prettyDOM,
15
- within,
8
+ queries,
16
9
  RenderResult,
10
+ act as rtlAct,
11
+ fireEvent as rtlFireEvent,
17
12
  screen as rtlScreen,
18
13
  Screen,
14
+ render as testingLibraryRender,
15
+ within,
19
16
  } from '@testing-library/react/pure';
20
17
  import { userEvent } from '@testing-library/user-event';
18
+ import * as React from 'react';
19
+ import * as ReactDOMServer from 'react-dom/server';
21
20
  import { useFakeTimers } from 'sinon';
22
21
  import reactMajor from './reactMajor';
23
22
 
@@ -114,8 +113,8 @@ class DispatchingProfiler implements Profiler {
114
113
 
115
114
  private renders: RenderMark[] = [];
116
115
 
117
- constructor(test: import('mocha').Test) {
118
- this.id = test.fullTitle();
116
+ constructor(id: string) {
117
+ this.id = id;
119
118
  }
120
119
 
121
120
  onRender: Profiler['onRender'] = (
@@ -193,18 +192,6 @@ const [
193
192
  },
194
193
  );
195
194
 
196
- const queryAllByMuiTest = queryHelpers.queryAllByAttribute.bind(null, 'data-mui-test');
197
- const [queryByMuiTest, getAllByMuiTest, getByMuiTest, findAllByMuiTest, findByMuiTest] =
198
- buildQueries(
199
- queryAllByMuiTest,
200
- function getMultipleError(container, dataMuiTest) {
201
- return `Found multiple elements with the data-mui-test attribute of: ${dataMuiTest}`;
202
- },
203
- function getMissingError(container, dataMuiTest) {
204
- return `Found no element with the data-mui-test attribute of: ${dataMuiTest}`;
205
- },
206
- );
207
-
208
195
  const customQueries = {
209
196
  queryDescriptionOf,
210
197
  queryAllDescriptionsOf,
@@ -212,30 +199,6 @@ const customQueries = {
212
199
  getAllDescriptionsOf,
213
200
  findDescriptionOf,
214
201
  findAllDescriptionsOf,
215
- /**
216
- * @deprecated Use `queryAllByTestId` instead
217
- */
218
- queryAllByMuiTest,
219
- /**
220
- * @deprecated Use `queryByTestId` instead
221
- */
222
- queryByMuiTest,
223
- /**
224
- * @deprecated Use `getAllByTestId` instead
225
- */
226
- getAllByMuiTest,
227
- /**
228
- * @deprecated Use `getByTestId` instead
229
- */
230
- getByMuiTest,
231
- /**
232
- * @deprecated Use `findAllByTestId` instead
233
- */
234
- findAllByMuiTest,
235
- /**
236
- * @deprecated Use `findByTestId` instead
237
- */
238
- findByMuiTest,
239
202
  };
240
203
 
241
204
  interface RenderConfiguration {
@@ -375,11 +338,88 @@ export interface Clock {
375
338
 
376
339
  export type ClockConfig = undefined | number | Date;
377
340
 
341
+ const isVitest =
342
+ // VITEST is present on the environment when not in browser mode.
343
+ process.env.VITEST === 'true' ||
344
+ // VITEST_BROWSER_DEBUG is present on vitest in browser mode.
345
+ typeof process.env.VITEST_BROWSER_DEBUG !== 'undefined';
346
+
347
+ function createVitestClock(
348
+ defaultMode: 'fake' | 'real',
349
+ config: ClockConfig,
350
+ options: Exclude<Parameters<typeof useFakeTimers>[0], number | Date>,
351
+ vi: any,
352
+ ): Clock {
353
+ if (defaultMode === 'fake') {
354
+ beforeEach(() => {
355
+ vi.useFakeTimers(options);
356
+ if (config) {
357
+ vi.setSystemTime(config);
358
+ }
359
+ });
360
+ afterEach(() => {
361
+ vi.useRealTimers();
362
+ });
363
+ } else {
364
+ beforeEach(() => {
365
+ if (config) {
366
+ vi.setSystemTime(config);
367
+ }
368
+ });
369
+ afterEach(() => {
370
+ vi.useRealTimers();
371
+ });
372
+ }
373
+
374
+ return {
375
+ withFakeTimers: () => {
376
+ beforeEach(() => {
377
+ vi.useFakeTimers(options);
378
+ });
379
+ afterEach(() => {
380
+ vi.useRealTimers();
381
+ });
382
+ },
383
+ runToLast: () => {
384
+ traceSync('runToLast', () => {
385
+ rtlAct(() => {
386
+ vi.runOnlyPendingTimers();
387
+ });
388
+ });
389
+ },
390
+ isReal() {
391
+ return !vi.isFakeTimers();
392
+ },
393
+ restore() {
394
+ vi.useRealTimers();
395
+ },
396
+ tick(timeoutMS: number) {
397
+ traceSync('tick', () => {
398
+ rtlAct(() => {
399
+ vi.advanceTimersByTime(timeoutMS);
400
+ });
401
+ });
402
+ },
403
+ runAll() {
404
+ traceSync('runAll', () => {
405
+ rtlAct(() => {
406
+ vi.runAllTimers();
407
+ });
408
+ });
409
+ },
410
+ };
411
+ }
412
+
378
413
  function createClock(
379
414
  defaultMode: 'fake' | 'real',
380
415
  config: ClockConfig,
381
- options?: Exclude<Parameters<typeof useFakeTimers>[0], number | Date>,
416
+ options: Exclude<Parameters<typeof useFakeTimers>[0], number | Date>,
417
+ vi: any,
382
418
  ): Clock {
419
+ if (isVitest) {
420
+ return createVitestClock(defaultMode, config, options, vi);
421
+ }
422
+
383
423
  let clock: ReturnType<typeof useFakeTimers> | null = null;
384
424
 
385
425
  let mode = defaultMode;
@@ -467,6 +507,11 @@ export interface CreateRendererOptions extends Pick<RenderOptions, 'strict' | 's
467
507
  clock?: 'fake' | 'real';
468
508
  clockConfig?: ClockConfig;
469
509
  clockOptions?: Parameters<typeof createClock>[2];
510
+ /**
511
+ * Vitest needs to be injected because this file is transpiled to commonjs and vitest is an esm module.
512
+ * @default {}
513
+ */
514
+ vi?: any;
470
515
  }
471
516
 
472
517
  export function createRenderer(globalOptions: CreateRendererOptions = {}): Renderer {
@@ -475,11 +520,12 @@ export function createRenderer(globalOptions: CreateRendererOptions = {}): Rende
475
520
  clockConfig,
476
521
  strict: globalStrict = true,
477
522
  strictEffects: globalStrictEffects = globalStrict,
523
+ vi = (globalThis as any).vi || {},
478
524
  clockOptions,
479
525
  } = globalOptions;
480
526
  // save stack to re-use in test-hooks
481
527
  const { stack: createClientRenderStack } = new Error();
482
- const clock = createClock(clockMode, clockConfig, clockOptions);
528
+ const clock = createClock(clockMode, clockConfig, clockOptions, vi);
483
529
 
484
530
  /**
485
531
  * Flag whether `createRenderer` was called in a suite i.e. describe() block.
@@ -531,13 +577,22 @@ export function createRenderer(globalOptions: CreateRendererOptions = {}): Rende
531
577
  throw error;
532
578
  }
533
579
 
534
- const test = this.currentTest;
535
- if (test === undefined) {
580
+ let id: string | null = null;
581
+
582
+ if (isVitest) {
583
+ // @ts-expect-error
584
+ id = expect.getState().currentTestName;
585
+ } else {
586
+ id = this.currentTest?.fullTitle() ?? null;
587
+ }
588
+
589
+ if (!id) {
536
590
  throw new Error(
537
591
  'Unable to find the currently running test. This is a bug with the client-renderer. Please report this issue to a maintainer.',
538
592
  );
539
593
  }
540
- profiler = new UsedProfiler(test);
594
+
595
+ profiler = new UsedProfiler(id);
541
596
 
542
597
  emotionCache = createEmotionCache({ key: 'emotion-client-render' });
543
598
 
@@ -1,6 +1,6 @@
1
1
  /* eslint-env mocha */
2
- import * as React from 'react';
3
2
  import { expect } from 'chai';
3
+ import * as React from 'react';
4
4
  import createDescribe from './createDescribe';
5
5
  import { MuiRenderResult } from './createRenderer';
6
6
 
@@ -63,6 +63,10 @@ export interface ConformanceOptions {
63
63
  testLegacyComponentsProp?: boolean;
64
64
  slots?: Record<string, SlotTestingOptions>;
65
65
  ThemeProvider?: React.ElementType;
66
+ /**
67
+ * If provided, the component will be tested by the `DefaultPropsProvider` (in addition to the ThemeProvider).
68
+ */
69
+ DefaultPropsProvider?: React.ElementType;
66
70
  createTheme?: (arg: any) => any;
67
71
  }
68
72
 
@@ -611,6 +615,41 @@ function testThemeDefaultProps(
611
615
  expect(container.firstChild).to.have.attribute(testProp, 'testProp');
612
616
  });
613
617
  });
618
+
619
+ describe('default props provider:', () => {
620
+ it('respect custom default props', async function test() {
621
+ const testProp = 'data-id';
622
+ const { muiName, render, DefaultPropsProvider } = getOptions();
623
+
624
+ if (!DefaultPropsProvider) {
625
+ this.skip();
626
+ }
627
+
628
+ if (!muiName) {
629
+ throwMissingPropError('muiName');
630
+ }
631
+
632
+ if (!render) {
633
+ throwMissingPropError('render');
634
+ }
635
+
636
+ const { container } = await render(
637
+ <DefaultPropsProvider
638
+ value={{
639
+ [muiName]: {
640
+ defaultProps: {
641
+ [testProp]: 'testProp',
642
+ },
643
+ },
644
+ }}
645
+ >
646
+ {element}
647
+ </DefaultPropsProvider>,
648
+ );
649
+
650
+ expect(container.firstChild).to.have.attribute(testProp, 'testProp');
651
+ });
652
+ });
614
653
  }
615
654
 
616
655
  /**
@@ -622,9 +661,11 @@ function testThemeStyleOverrides(
622
661
  getOptions: () => ConformanceOptions,
623
662
  ) {
624
663
  describe('theme style overrides:', () => {
625
- it("respect theme's styleOverrides custom state", async function test() {
664
+ it("respect theme's styleOverrides custom state", async function test(t = {}) {
626
665
  if (/jsdom/.test(window.navigator.userAgent)) {
627
- this.skip();
666
+ // @ts-ignore
667
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
668
+ this?.skip?.() ?? t?.skip();
628
669
  }
629
670
  const { muiName, testStateOverrides, render, ThemeProvider, createTheme } = getOptions();
630
671
 
@@ -677,9 +718,11 @@ function testThemeStyleOverrides(
677
718
  expect(container.firstChild).to.toHaveComputedStyle(testStyle);
678
719
  });
679
720
 
680
- it("respect theme's styleOverrides slots", async function test() {
721
+ it("respect theme's styleOverrides slots", async function test(t = {}) {
681
722
  if (/jsdom/.test(window.navigator.userAgent)) {
682
- this.skip();
723
+ // @ts-ignore
724
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
725
+ this?.skip?.() ?? t?.skip();
683
726
  }
684
727
 
685
728
  const {
@@ -788,9 +831,11 @@ function testThemeStyleOverrides(
788
831
  }
789
832
  });
790
833
 
791
- it('overrideStyles does not replace each other in slots', async function test() {
834
+ it('overrideStyles does not replace each other in slots', async function test(t = {}) {
792
835
  if (/jsdom/.test(window.navigator.userAgent)) {
793
- this.skip();
836
+ // @ts-ignore
837
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
838
+ this?.skip?.() ?? t?.skip();
794
839
  }
795
840
 
796
841
  const { muiName, classes, testStateOverrides, render, ThemeProvider, createTheme } =
@@ -866,9 +911,11 @@ function testThemeStyleOverrides(
866
911
  */
867
912
  function testThemeVariants(element: React.ReactElement<any>, getOptions: () => ConformanceOptions) {
868
913
  describe('theme variants:', () => {
869
- it("respect theme's variants", async function test() {
914
+ it("respect theme's variants", async function test(t = {}) {
870
915
  if (/jsdom/.test(window.navigator.userAgent)) {
871
- this.skip();
916
+ // @ts-ignore
917
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
918
+ this?.skip?.() ?? t?.skip();
872
919
  }
873
920
 
874
921
  const { muiName, testVariantProps, render, ThemeProvider, createTheme } = getOptions();
@@ -921,9 +968,11 @@ function testThemeVariants(element: React.ReactElement<any>, getOptions: () => C
921
968
  expect(getByTestId('without-props')).not.to.toHaveComputedStyle(testStyle);
922
969
  });
923
970
 
924
- it('supports custom variant', async function test() {
971
+ it('supports custom variant', async function test(t = {}) {
925
972
  if (/jsdom/.test(window.navigator.userAgent)) {
926
- this.skip();
973
+ // @ts-ignore
974
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
975
+ this?.skip?.() ?? t?.skip();
927
976
  }
928
977
 
929
978
  const { muiName, testCustomVariant, render, ThemeProvider, createTheme } = getOptions();
@@ -974,12 +1023,14 @@ function testThemeCustomPalette(
974
1023
  getOptions: () => ConformanceOptions,
975
1024
  ) {
976
1025
  describe('theme extended palette:', () => {
977
- it('should render without errors', function test() {
1026
+ it('should render without errors', function test(t = {}) {
978
1027
  const { render, ThemeProvider, createTheme } = getOptions();
979
1028
  if (!/jsdom/.test(window.navigator.userAgent) || !render || !ThemeProvider || !createTheme) {
980
- this.skip();
1029
+ // @ts-ignore
1030
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
1031
+ this?.skip?.() ?? t?.skip();
981
1032
  }
982
-
1033
+ // @ts-ignore
983
1034
  const theme = createTheme({
984
1035
  palette: {
985
1036
  custom: {
@@ -995,7 +1046,7 @@ function testThemeCustomPalette(
995
1046
  },
996
1047
  },
997
1048
  });
998
-
1049
+ // @ts-ignore
999
1050
  expect(() => render(<ThemeProvider theme={theme}>{element}</ThemeProvider>)).not.to.throw();
1000
1051
  });
1001
1052
  });
@@ -1030,7 +1081,7 @@ function describeConformance(
1030
1081
  beforeEach(() => {
1031
1082
  originalMatchmedia = window.matchMedia;
1032
1083
  // Create mocks of localStorage getItem and setItem functions
1033
- Object.defineProperty(global, 'localStorage', {
1084
+ Object.defineProperty(globalThis, 'localStorage', {
1034
1085
  value: {
1035
1086
  getItem: (key: string) => storage[key],
1036
1087
  setItem: (key: string, value: string) => {
@@ -0,0 +1,9 @@
1
+ // Shim for vitest describe.skipIf to be able to run mocha and vitest side-by-side
2
+ // TODO: Remove after migration to vitest is complete
3
+ const describeSkipIf: (condition: boolean) => Mocha.PendingSuiteFunction =
4
+ (describe as any).skipIf ??
5
+ function describeSkipIf(condition: boolean) {
6
+ return condition ? describe.skip : describe;
7
+ };
8
+
9
+ export default describeSkipIf;
package/src/index.ts CHANGED
@@ -15,6 +15,7 @@ export {} from './initMatchers';
15
15
  export * as fireDiscreteEvent from './fireDiscreteEvent';
16
16
  export { default as flushMicrotasks } from './flushMicrotasks';
17
17
  export { default as reactMajor } from './reactMajor';
18
+ export { default as describeSkipIf } from './describeSkipIf';
18
19
 
19
20
  /**
20
21
  * Set to true if console logs during [lifecycles that are invoked twice in `React.StrictMode`](https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects) are suppressed.
@@ -43,13 +43,12 @@ describe('custom matchers', () => {
43
43
  expect(caughtError.stack).to.include(
44
44
  'Could not match the following console.error calls. ' +
45
45
  "Make sure previous actions didn't call console.error by wrapping them in expect(() => {}).not.toErrorDev(): \n\n" +
46
- ' - "expected message"\n' +
47
- ' at Context.', // `Context.it` in node 12.x, `Context.<anonymous>` in later node version
46
+ ' - "expected message"\n',
48
47
  );
49
48
  // check that the top stackframe points to this test
50
49
  // if this test is moved to another file the next assertion fails
51
50
  expect(caughtError.stack).to.match(
52
- /- "expected message"\s+at Context\.(<anonymous>|it) \(.+\/initMatchers\.test\.js:\d+:\d+\)/,
51
+ /- "expected message"\s+at .*\/initMatchers\.test\.js:\d+:\d+/,
53
52
  );
54
53
  });
55
54
 
@@ -65,13 +64,12 @@ describe('custom matchers', () => {
65
64
  expect(caughtError.stack).to.include(
66
65
  'Recorded unexpected console.error calls: \n\n' +
67
66
  ' - Expected #1 "expected message" to be included in \n' +
68
- '"expected Message"\n' +
69
- ' at callback',
67
+ '"expected Message"\n',
70
68
  );
71
69
  // check that the top stackframe points to this test
72
70
  // if this test is moved to another file the next assertion fails
73
71
  expect(caughtError.stack).to.match(
74
- /"expected Message"\s+at callback \(.+\/initMatchers\.test\.js:\d+:\d+\)/,
72
+ /"expected Message"\s+at .*\/initMatchers\.test\.js:\d+:\d+/,
75
73
  );
76
74
  });
77
75