1
+
2
+
3
+ const initFolders = ( path : string , appName : string ) => {
4
+ let dir = path ;
5
+ dir = `${ dir } /${ appName } ` ;
6
+ if ( ! window . api . existsSync ( `${ dir } /__mocks__` ) ) {
7
+ window . api . mkdirSync ( `${ dir } /__mocks__` ) ;
8
+ }
9
+ if ( ! window . api . existsSync ( `${ dir } /__tests__` ) ) {
10
+ window . api . mkdirSync ( `${ dir } /__tests__` ) ;
11
+ }
12
+ }
13
+
14
+ async function createJestConfigFile ( path : String , appName : String ) {
15
+ const filePath :string = `${ path } /${ appName } /jest.config.js` ;
16
+ const data :string = `
17
+ module.exports = {
18
+ moduleFileExtensions: [
19
+ "ts",
20
+ "tsx",
21
+ "js"
22
+ ],
23
+ transform: {
24
+ "^.+\\.tsx?$": "ts-jest",
25
+ "^.+\\.jsx?$": "<rootDir>/jest-preprocess.js",
26
+
27
+ },
28
+ testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.([tj]sx?)$",
29
+ globals: {
30
+ "ts-jest": {
31
+ babelConfig: true,
32
+ tsconfig: "jest.tsconfig.json"
33
+ }
34
+ },
35
+ coveragePathIgnorePatterns: [
36
+ "/node_modules/",
37
+ "enzyme.js"
38
+ ],
39
+ setupFilesAfterEnv: ["<rootDir>/enzyme.js"],
40
+ coverageReporters: [
41
+ "json",
42
+ "lcov",
43
+ "text",
44
+ "text-summary"
45
+ ],
46
+ moduleNameMapper: {
47
+ "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/file-mock.js",
48
+ "\\.(css|less|scss)$": "identity-obj-proxy"
49
+ }
50
+ }
51
+ ` ;
52
+ window . api . writeFile ( filePath , data , err => {
53
+ if ( err ) {
54
+ console . log ( 'createTestSuiteNext.util createJestConfigFile error:' , err . message ) ;
55
+ } else {
56
+ console . log ( 'createTestSuitNext.util createJestConfigFile written successfully' ) ;
57
+ }
58
+ } ) ;
59
+ } ;
60
+
61
+ async function createJestTsconfigJsonFile ( path : String , appName : String ) {
62
+ console . log ( 'got in create jest tsconfig' ) ;
63
+ const filePath :string = `${ path } /${ appName } /jest.tsconfig.json` ;
64
+ const data :string = `
65
+ {
66
+ "compilerOptions": {
67
+ "module": "commonjs",
68
+ "target": "esnext",
69
+ "jsx": "react",
70
+ "sourceMap": false,
71
+ "experimentalDecorators": true,
72
+ "noImplicitUseStrict": true,
73
+ "moduleResolution": "node",
74
+ "esModuleInterop": true,
75
+ "allowSyntheticDefaultImports": true,
76
+ "lib": [
77
+ "es2017",
78
+ "dom"
79
+ ],
80
+ "typeRoots": [
81
+ "node_modules/@types"
82
+ ]
83
+ },
84
+ "exclude": [
85
+ "node_modules",
86
+ "out",
87
+ ".next"
88
+ ]
89
+ }
90
+ ` ;
91
+ console . log ( data ) ;
92
+ window . api . writeFile ( filePath , data , err => {
93
+ if ( err ) {
94
+ console . log ( 'createTestSuiteNext.util createJestTsconfigJsonFile error:' , err . message ) ;
95
+ } else {
96
+ console . log ( 'createTestSuitNext.util createJestTsconfigJsonFile written successfully' ) ;
97
+ }
98
+ } ) ;
99
+ } ;
100
+
101
+
102
+ async function createJestPreprocessFile ( path : string , appName : string ) {
103
+ const filePath : string = `${ path } /${ appName } /jest-preprocess.js` ;
104
+ const data :string = `
105
+ const babelOptions = {
106
+ presets: ["next/babel"],
107
+ }
108
+
109
+ module.exports = require("babel-jest").default.createTransformer(babelOptions)` ;
110
+
111
+ window . api . writeFile ( filePath , data , err => {
112
+ if ( err ) {
113
+ console . log ( 'createTestSuite.util createJestPreprocessFile error:' , err . message ) ;
114
+ } else {
115
+ console . log ( 'createTestSuit.util createJestPreprocessFile written successfully' ) ;
116
+ }
117
+ } ) ;
118
+ }
119
+
120
+ async function createEnzymeFile ( path : string , appName : string ) {
121
+ const filePath : string = `${ path } /${ appName } /enzyme.js` ;
122
+ const data :string = `const Adapter = require('enzyme-adapter-react-16');
123
+ require('enzyme').configure({adapter: new Adapter()});` ;
124
+
125
+ window . api . writeFile ( filePath , data , err => {
126
+ if ( err ) {
127
+ console . log ( 'createTestSuite.util createEnzymeFile error:' , err . message ) ;
128
+ } else {
129
+ console . log ( 'createTestSuit.util createEnzymeFile written successfully' ) ;
130
+ }
131
+ } ) ;
132
+ }
133
+
134
+ async function createComponentTests ( path : string , appName : string , components : Component [ ] ) {
135
+ const filePath : string = `${ path } /${ appName } /__tests__/test.tsx` ;
136
+ console . log ( JSON . stringify ( components ) )
137
+ console . log ( components ) ;
138
+
139
+ let data :string = `
140
+ import { shallow } from 'enzyme'
141
+ import React from 'react';
142
+ ` ;
143
+
144
+ components . forEach ( page => {
145
+ console . log ( page ) ;
146
+ let importString = '' ;
147
+ if ( page . isPage ) {
148
+ importString = `
149
+ import ${ capitalize ( page . name ) } from "../pages/${ page . name } ";` ;
150
+ data = data + importString ;
151
+ } else {
152
+ importString = `
153
+ import ${ capitalize ( page . name ) } from "../components/${ page . name } ";` ;
154
+ data = data + importString ;
155
+ }
156
+ } )
157
+
158
+ //let describe = `describe("${page.name}", () => {`
159
+ components . forEach ( page => {
160
+ data = data + `
161
+
162
+ describe("${ capitalize ( page . name ) } ", () => {`
163
+
164
+
165
+ data = data + `
166
+ it('renders snapshots, too', () => {
167
+ const wrapper = shallow(< ${ capitalize ( page . name ) } />)
168
+ expect(wrapper).toMatchSnapshot()
169
+ })`
170
+
171
+
172
+ data = data + `
173
+ });`
174
+ } )
175
+
176
+
177
+
178
+ window . api . writeFile ( filePath , data , err => {
179
+ if ( err ) {
180
+ console . log ( 'createTestSuiteNext.util createComponentTests error:' , err . message ) ;
181
+ } else {
182
+ console . log ( 'createTestSuitNext.util createComponentTests written successfully' ) ;
183
+ }
184
+ } ) ;
185
+ }
186
+
187
+ const capitalize = ( string : string ) => {
188
+ return string . charAt ( 0 ) . toUpperCase ( ) + string . slice ( 1 ) ;
189
+ }
190
+
191
+
192
+ async function createTestSuite ( {
193
+ path,
194
+ appName,
195
+ components,
196
+ rootComponents,
197
+ testchecked,
198
+ } : {
199
+ path : string ;
200
+ appName : string ;
201
+ components : Component [ ] ;
202
+ rootComponents : number [ ] ;
203
+ testchecked : boolean ;
204
+ } ) {
205
+ console . log ( 'in the createGatsbyApplication util' ) ;
206
+ console . log ( 'testchecked: ' , testchecked ) ;
207
+
208
+ await initFolders ( path , appName ) ;
209
+ // await createMocksFiles(path, appName);
210
+ // await createTestsFiles(path, appName);
211
+ await createJestConfigFile ( path , appName ) ;
212
+ await createJestTsconfigJsonFile ( path , appName ) ;
213
+ await createJestPreprocessFile ( path , appName ) ;
214
+ await createEnzymeFile ( path , appName ) ;
215
+ await createComponentTests ( path , appName , components ) ;
216
+ }
217
+
218
+ export default createTestSuite ;
0 commit comments