@@ -2,28 +2,115 @@ import { extendStoriesFromStoryFile } from "./extend-stories";
22import { StorybookStory } from "./types" ;
33
44describe ( "storybook/story-test-runner/extend-stories" , ( ) => {
5- it ( "should log warning failed storyfile import" , async ( ) => {
5+ const mkRequireStub_ = (
6+ impl ?: Parameters < ( typeof jest ) [ "fn" ] > [ 0 ] ,
7+ ) : jest . MockedFunction < ( typeof globalThis ) [ "require" ] > => {
8+ return jest . fn ( impl ) as unknown as jest . MockedFunction < ( typeof globalThis ) [ "require" ] > ;
9+ } ;
10+
11+ it ( "should log warning failed storyfile import" , ( ) => {
612 jest . spyOn ( console , "warn" ) . mockImplementation ( jest . fn ) ;
13+ const requireFn = mkRequireStub_ ( ( ) => {
14+ throw new Error ( "some error message" ) ;
15+ } ) ;
716 const stories = [ { name : "foo" , absolutePath : "not/existing.ts" } ] as StorybookStory [ ] ;
817
9- extendStoriesFromStoryFile ( stories ) ;
18+ extendStoriesFromStoryFile ( stories , { requireFn } ) ;
1019
1120 const expectedMsg = [
1221 '"testplane" section is ignored in storyfile "not/existing.ts", because the file could not be read:' ,
13- "Error: Cannot find module 'not/existing.ts' from 'src/storybook/story-test-runner/extend-stories.ts' " ,
22+ "Error: some error message " ,
1423 "There could be other story files. " ,
1524 "Set 'TESTPLANE_STORYBOOK_DISABLE_STORY_REQUIRE_WARNING' environment variable to hide this warning" ,
1625 ] . join ( "\n" ) ;
1726 expect ( console . warn ) . toBeCalledWith ( expectedMsg ) ;
1827 } ) ;
1928
20- it ( "should fallback, when could not read story file" , async ( ) => {
29+ it ( "should fallback, when could not read story file" , ( ) => {
30+ const requireFn = mkRequireStub_ ( ( ) => {
31+ throw new Error ( "file does not exist" ) ;
32+ } ) ;
2133 const stories = [ { name : "foo" , absolutePath : "not/existing.js" } ] as StorybookStory [ ] ;
2234
23- const extendedStories = extendStoriesFromStoryFile ( stories ) ;
35+ const extendedStories = extendStoriesFromStoryFile ( stories , { requireFn } ) ;
2436
2537 expect ( extendedStories [ 0 ] . skip ) . toBe ( false ) ;
2638 expect ( extendedStories [ 0 ] . assertViewOpts ) . toEqual ( { } ) ;
2739 expect ( extendedStories [ 0 ] . browserIds ) . toBe ( null ) ;
2840 } ) ;
41+
42+ it ( "should overlay story configs over file configs" , ( ) => {
43+ const customTests = { } ;
44+ const defaultExport = {
45+ testplane : { browserIds : [ "firefox" ] } ,
46+ testplaneConfig : {
47+ skip : true ,
48+ browserIds : [ "chrome" ] ,
49+ assertViewOpts : { tolerance : 10 , ignoreDiffPixelCount : 10 } ,
50+ autoScreenshotStorybookGlobals : { dark : { theme : "dark" } } ,
51+ } ,
52+ } ;
53+ const fooExport = {
54+ testplane : customTests ,
55+ testplaneConfig : { skip : false , autoScreenshots : false , assertViewOpts : { ignoreElements : [ "foobar" ] } } ,
56+ } ;
57+ const requireFn = mkRequireStub_ ( ) . mockReturnValue ( { default : defaultExport , foo : fooExport } ) ;
58+ const stories = [ { name : "foo" , absolutePath : "not/existing.js" } ] as StorybookStory [ ] ;
59+
60+ const extendedStories = extendStoriesFromStoryFile ( stories , { requireFn } ) ;
61+
62+ expect ( extendedStories ) . toMatchObject ( [
63+ {
64+ name : "foo" ,
65+ absolutePath : "not/existing.js" ,
66+ extraTests : { } ,
67+ skip : false ,
68+ browserIds : [ "chrome" ] ,
69+ assertViewOpts : {
70+ ignoreElements : [ "foobar" ] ,
71+ tolerance : 10 ,
72+ ignoreDiffPixelCount : 10 ,
73+ } ,
74+ autoScreenshots : false ,
75+ autoScreenshotStorybookGlobals : { dark : { theme : "dark" } } ,
76+ } ,
77+ ] ) ;
78+ } ) ;
79+
80+ it ( "should overlay story configs over default configs" , ( ) => {
81+ const customTests = { } ;
82+ const fooExport = {
83+ testplane : customTests ,
84+ testplaneConfig : { skip : false , autoScreenshots : false , assertViewOpts : { ignoreElements : [ "foobar" ] } } ,
85+ } ;
86+ const requireFn = mkRequireStub_ ( ) . mockReturnValue ( { default : { } , foo : fooExport } ) ;
87+ const stories = [ { name : "foo" , absolutePath : "not/existing.js" } ] as StorybookStory [ ] ;
88+
89+ const extendedStories = extendStoriesFromStoryFile ( stories , { requireFn } ) ;
90+
91+ expect ( extendedStories ) . toMatchObject ( [
92+ {
93+ name : "foo" ,
94+ absolutePath : "not/existing.js" ,
95+ extraTests : { } ,
96+ skip : false ,
97+ browserIds : null ,
98+ assertViewOpts : {
99+ ignoreElements : [ "foobar" ] ,
100+ } ,
101+ autoScreenshots : false ,
102+ autoscreenshotSelector : null ,
103+ autoScreenshotStorybookGlobals : { } ,
104+ } ,
105+ ] ) ;
106+ } ) ;
107+
108+ it ( "should fallback reading story configs from deprecated testplane property" , ( ) => {
109+ const stories = [ { name : "foo" , absolutePath : "not/existing.js" } ] as StorybookStory [ ] ;
110+ const requireFn = mkRequireStub_ ( ) . mockReturnValue ( { default : { testplane : { skip : true } } , foo : { } } ) ;
111+
112+ const extendedStories = extendStoriesFromStoryFile ( stories , { requireFn } ) ;
113+
114+ expect ( extendedStories ) . toMatchObject ( [ { name : "foo" , skip : true } ] ) ;
115+ } ) ;
29116} ) ;
0 commit comments