Skip to content

Commit 5753c56

Browse files
committed
docs: Update migration howto with jest @src alias
The migration howto was missing the @src alias in jest configuration, since it's no longer provided by the default in frontend-base. Add the finding that `tsc-alias` needs to be called after assets are copied. Finally, document the fix to the site.config.test.tsx circular dependency when mocking `@openedx/frontend-base` itself.
1 parent 094119f commit 5753c56

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

docs/how_tos/migrate-frontend-app.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,25 @@ Also:
156156
> **Why change `fedx-scripts` to `openedx`?**
157157
> A few reasons. One, the Open edX project shouldn't be using the name of an internal community of practice at edX for its frontend tooling. Two, some dependencies of your MFE invariably still use frontend-build for their own build needs. This means that they already installed `fedx-scripts` into your `node_modules/.bin` folder. Only one version can be in there, so we need a new name. Seemed like a great time for a naming refresh. |
158158
159-
Last but not least, add `clean:` and `build:` targets to your `Makefile`. The build target compiles TypeScript to JavaScript, uses `tsc-alias` to rewrite `@src` path aliases to relative paths, and copies all SCSS files from `src/` into `dist/` preserving directory structure:
159+
Last but not least, add `clean:` and `build:` targets to your `Makefile`. The build target compiles TypeScript to JavaScript, copies all SCSS and asset files from `src/` into `dist/` preserving directory structure, and finally uses `tsc-alias` to rewrite `@src` path aliases to relative paths:
160160

161161
```makefile
162162
clean:
163163
rm -rf dist
164164

165165
build: clean
166166
tsc --project tsconfig.build.json
167-
tsc-alias -p tsconfig.build.json
168-
find src -type f -name '*.scss' -exec sh -c '\
167+
find src -type f \( -name '*.scss' -o -path '*/assets/*' \) -exec sh -c '\
169168
for f in "$$@"; do \
170169
d="dist/$${f#src/}"; \
171170
mkdir -p "$$(dirname "$$d")"; \
172171
cp "$$f" "$$d"; \
173172
done' sh {} +
173+
tsc-alias -p tsconfig.build.json
174174
```
175175

176+
Note that the `find` command copies all files under `assets/` directories regardless of type, so you don't need to enumerate asset extensions. Also note that `tsc-alias` runs after the copy step so that it can resolve `@src` aliases pointing to asset files. If it ran before, it wouldn't find them and would omit them from the relative path conversion.
177+
176178
Other package.json edits
177179
------------------------
178180

@@ -372,12 +374,13 @@ module.exports = createConfig('test', {
372374
})
373375
```
374376

375-
Jest test suites that test React components that import SVG and files must add mocks for those filetypes. This can be accomplished by adding the following module name mappers to jest.config.js:
377+
Jest test suites that test React components that import SVG and other assets (such as PNGs) must add mocks for those filetypes. This can be accomplished by adding module name mappers to jest.config.js. Just make sure they come before the `@src` alias, which must also be added here if you're using it:
376378

377379
```js
378380
moduleNameMapper: {
379381
'\\.svg$': '<rootDir>/src/__mocks__/svg.js',
380-
'\\.(jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/src/__mocks__/file.js',
382+
'\\.png$': '<rootDir>/src/__mocks__/file.js',
383+
'^@src/(.*)$': '<rootDir>/src/$1',
381384
},
382385
```
383386

@@ -420,7 +423,8 @@ module.exports = createConfig('test', {
420423
],
421424
moduleNameMapper: {
422425
'\\.svg$': '<rootDir>/src/__mocks__/svg.js',
423-
'\\.(jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/src/__mocks__/file.js',
426+
'\\.png$': '<rootDir>/src/__mocks__/file.js',
427+
'^@src/(.*)$': '<rootDir>/src/$1',
424428
},
425429
});
426430
```
@@ -800,7 +804,7 @@ Once you've verified your test suite still works, you should delete the `.env.te
800804
A sample `site.config.test.tsx` file:
801805

802806
```js
803-
import { EnvironmentTypes, SiteConfig } from '@openedx/frontend-base';
807+
import type { SiteConfig } from '@openedx/frontend-base';
804808

805809
const siteConfig: SiteConfig = {
806810
siteId: 'test',
@@ -809,7 +813,9 @@ const siteConfig: SiteConfig = {
809813
lmsBaseUrl: 'http://localhost:18000',
810814
loginUrl: 'http://localhost:18000/login',
811815
logoutUrl: 'http://localhost:18000/logout',
812-
environment: EnvironmentTypes.TEST,
816+
// Use 'test' instead of EnvironmentTypes.TEST to break a circular dependency
817+
// when mocking `@openedx/frontend-base` itself.
818+
environment: 'test' as SiteConfig['environment'],
813819
apps: [{
814820
appId: 'test-app',
815821
routes: [{

0 commit comments

Comments
 (0)