Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 8080852

Browse files
authored
fix: direct file-loader at __root (#298)
this allows relative resources like fonts to be served by Cypress
1 parent 9f799be commit 8080852

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

examples/react-scripts/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ npm test
1212
```
1313

1414
![App test](images/app-test.png)
15+
16+
The spec [src/Logo.cy-spec.js](src/Logo.cy-spec.js) directly imports SVG into React spec file. The spec [src/resources.cy-spec.js](src/resources.cy-spec.js) confirm that static resources like SVG and fonts load correctly.

examples/react-scripts/src/App.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
justify-content: center;
2323
font-size: calc(10px + 2vmin);
2424
color: white;
25+
font-family: 'MyFont';
2526
}
2627

2728
.App-link {
@@ -36,3 +37,8 @@
3637
transform: rotate(360deg);
3738
}
3839
}
40+
41+
@font-face {
42+
font-family: 'MyFont';
43+
src: local('MyFont'), url(./Inter-Regular.woff) format('woff');
44+
}
131 KB
Binary file not shown.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import App from './App'
4+
import { mount } from 'cypress-react-unit-test'
5+
6+
describe('static resources', () => {
7+
const findResource = name => {
8+
return window.performance
9+
.getEntriesByType('resource')
10+
.find(item => item.name.endsWith(name))
11+
}
12+
13+
it.only('loads SVG', () => {
14+
// checking if a static resource like a font has loaded
15+
// based on the recipe "Waiting for static resource"
16+
// https://github.com/cypress-io/cypress-example-recipes#testing-the-dom
17+
mount(<App />)
18+
19+
// use wrap + .should() to retry the callback function
20+
cy.wrap().should(() => {
21+
const foundResource = findResource('logo.svg')
22+
expect(foundResource).to.have.property('initiatorType', 'img')
23+
})
24+
})
25+
26+
it('loads font', () => {
27+
// https://github.com/bahmutov/cypress-react-unit-test/issues/284
28+
mount(<App />)
29+
30+
cy.wrap().should(() => {
31+
const foundResource = findResource('Inter-Regular.woff')
32+
expect(foundResource).to.have.property('initiatorType', 'css')
33+
})
34+
})
35+
})

plugins/utils/add-image-redirect.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ function addImageRedirect(webpackOptions) {
1515
return
1616
}
1717

18+
debug('adding image redirect to %o', webpackOptions)
19+
1820
// we need to handle static images and redirect them to
1921
// the existing files. Cypress has fallthrough static server
2022
// for anything like "/_root/<path>" which is perfect - because
@@ -26,13 +28,28 @@ function addImageRedirect(webpackOptions) {
2628

2729
const imageRedirectLoaderRule = {
2830
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/, /\.svg$/],
29-
loader: require.resolve('./redirect-resource'),
31+
loader: require.resolve('./redirect-image-resource'),
3032
}
3133

3234
if (loaderRules) {
3335
debug('found oneOf rule %o', loaderRules.oneOf)
3436
debug('adding our static image loader')
3537
loaderRules.oneOf.unshift(imageRedirectLoaderRule)
38+
39+
// while we are here, let's change file loader
40+
// to point it at the /__root/... path
41+
const fileLoader = loaderRules.oneOf[loaderRules.oneOf.length - 1]
42+
if (
43+
fileLoader &&
44+
fileLoader.loader &&
45+
fileLoader.loader.includes('file-loader')
46+
) {
47+
if (fileLoader.options && fileLoader.options.name) {
48+
debug('setting file-loader to output /__root')
49+
fileLoader.options.name = '/__root/[path][name].[ext]'
50+
debug('file loader %o', fileLoader)
51+
}
52+
}
3653
} else {
3754
debug('could not find oneOf rules, inserting directly into rules')
3855
webpackOptions.module.rules.unshift(imageRedirectLoaderRule)
File renamed without changes.

0 commit comments

Comments
 (0)