Skip to content

Commit

Permalink
feat!: typescript configuration and jest v29 upgrade (#429)
Browse files Browse the repository at this point in the history
This introduces two main features:

* feat: Re-enable typescript for production builds (introduced directly in this `alpha` branch)
* feat!: updated jest to v29 (introduced originally via #415)

It also includes a series of minor workflow and dependency updates.

BREAKING CHANGE: Updating jest from v26 to v29 changes the default snapshotFormat.
  • Loading branch information
adamstankiewicz authored Apr 19, 2024
1 parent 9c0b275 commit 91c6ce4
Show file tree
Hide file tree
Showing 13 changed files with 2,367 additions and 2,927 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/commitlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ on:

jobs:
commitlint:
uses: openedx/.github/.github/workflows/commitlint.yml@master
runs-on: ubuntu-20.04
steps:
- name: Check out repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Check commits
uses: wagoid/commitlint-github-action@v5
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,25 @@ locally. To serve a production build locally:
attempt to run the build on the same port specified in the
`env.config.js` file.

## Local module configuration for TypeScript

#. Create file in repository `tsconfig.json`, with a clause `"extends": "@openedx/frontend-build"`
#. Set "rootDir" to the root of the source code folders
#. Set "include" to wildcard patterns specifying the subdirectories/files under rootDir where source code can be found
#. Include any wildcards under rootDir that should be excluded using "exclude"

```Sample json
{
"extends": "@edx/typescript-config",
"compilerOptions": {
"rootDir": ".",
"outDir": "dist"
},
"include": ["src/**/*"],
"exclude": ["dist", "node_modules"]
}
```

## Development

This project leverages the command line interface for webpack, jest,
Expand Down
3 changes: 2 additions & 1 deletion config/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const { babel } = require('../lib/presets');

module.exports = {
extends: '@edx/eslint-config',
parser: '@babel/eslint-parser',
plugins: ['@typescript-eslint'],
parser: '@typescript-eslint/parser',
parserOptions: {
requireConfigFile: true,
babelOptions: {
Expand Down
9 changes: 7 additions & 2 deletions config/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path');
const fs = require('fs');
const { jsWithTs: tsjPreset } = require('ts-jest/presets');

const presets = require('../lib/presets');

Expand All @@ -11,7 +12,10 @@ if (fs.existsSync(appEnvConfigPath)) {
}

module.exports = {
testURL: 'http://localhost/',
testEnvironment: 'jsdom',
testEnvironmentOptions: {
url: 'http://localhost/',
},
setupFiles: [
path.resolve(__dirname, 'jest/setupTest.js'),
],
Expand All @@ -33,11 +37,12 @@ module.exports = {
'/node_modules/(?!@(open)?edx)',
],
transform: {
'^.+\\.[t|j]sx?$': [
'^.+\\.jsx?$': [
'babel-jest',
{
configFile: presets.babel.resolvedFilepath,
},
],
...tsjPreset.transform,
},
};
2 changes: 1 addition & 1 deletion config/webpack.common.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
// the application being built.
'env.config': false,
},
extensions: ['.js', '.jsx'],
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
ignoreWarnings: [
// Ignore warnings raised by source-map-loader.
Expand Down
4 changes: 2 additions & 2 deletions config/webpack.prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ module.exports = merge(commonConfig, {
// The babel-loader transforms newer ES2015+ syntax to older ES5 for older browsers.
// Babel is configured with the .babelrc file at the root of the project.
{
test: /\.(js|jsx)$/,
test: /\.(js|jsx|ts|tsx)$/,
exclude: /node_modules\/(?!@(open)?edx)/,
use: {
loader: 'babel-loader',
options: {
configFile: presets.babel.resolvedFilepath,
configFile: presets['babel-typescript'].resolvedFilepath,
},
},
},
Expand Down
3 changes: 3 additions & 0 deletions example/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import config from 'env.config';
import Image from './Image';
import appleUrl, { ReactComponent as Apple } from './apple.svg';
import appleImg from './apple.jpg';

Expand All @@ -19,6 +20,8 @@ export default function App() {
</ul>
<h2>JSX parsing tests</h2>
<Apple style={{ width: '10rem' }} />
<h2>TSX parsing tests</h2>
<Image src={appleUrl} alt="appleFromTsx" style={{ width: '10rem' }} />
<h2>Asset import tests</h2>
<img src={appleUrl} alt="apple" style={{ width: '10rem' }} />
<img src={appleUrl} alt="apple" style={{ width: '10rem' }} />
Expand Down
16 changes: 16 additions & 0 deletions example/src/Image.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React, { CSSProperties } from 'react';

type ImageProps = {
src: string;
alt?: string;
style?: CSSProperties;
};

const Image = ({ alt, ...rest }:ImageProps) => <img alt={alt} {...rest} />;

const defaultProps = {
alt: undefined,
style: undefined,
};
Image.defaultProps = defaultProps;
export default Image;
20 changes: 16 additions & 4 deletions example/src/__snapshots__/App.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ exports[`Basic test should render 1`] = `
</h2>
<IconMock
style={
Object {
{
"width": "10rem",
}
}
/>
<h2>
TSX parsing tests
</h2>
<img
alt="appleFromTsx"
src="icon/mock/path"
style={
{
"width": "10rem",
}
}
Expand All @@ -46,7 +58,7 @@ exports[`Basic test should render 1`] = `
alt="apple"
src="icon/mock/path"
style={
Object {
{
"width": "10rem",
}
}
Expand All @@ -55,7 +67,7 @@ exports[`Basic test should render 1`] = `
alt="apple"
src="icon/mock/path"
style={
Object {
{
"width": "10rem",
}
}
Expand All @@ -65,7 +77,7 @@ exports[`Basic test should render 1`] = `
alt="apple"
src="test-file-stub"
style={
Object {
{
"width": "10rem",
}
}
Expand Down
16 changes: 16 additions & 0 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "dist"
},
"include": [
".eslintrc.js",
"env.config.js",
"src"
],
"exclude": [
"node_modules",
"dist",
]
}
Loading

0 comments on commit 91c6ce4

Please sign in to comment.