andywer / andywer/threads.js

Question: Jest + Typescript + threads.js how can it work together ?

Abierto
#269 15 comentarios 1 reacción 0 asignados Ver en GitHub
question
Lenguaje dominante
TypeScript
Estrellas
3.5k
Forks
173
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

I have implemented threads.js in my "vue + typescript + electron" project and it works, but my test now fails with the error:

```
pinguSync\src\backend\controller\main-list-manager\worker\main-list-search-id-worker.ts:1
import { expose } from 'threads/worker';
^^^^^^

SyntaxError: Cannot use import statement outside a moduleJest
```
"worker.ts":
```
import { expose } from 'threads/worker';
import Series from '../../objects/series';

const worker = function findSeriesById(buffer: Series[], id: string): Series | null {
return buffer.find(x => x.id === id) ?? null;
};

export type MainListSearchIdWorker = typeof worker;

expose(worker);
```
"main":
```
public static async findSeriesById(id: string): Promise {
const worker = await spawn(new Worker('./worker/main-list-search-id-worker.ts'));
return worker(MainListManager.getMainList(), id);
}
```

"test":
```
describe('test fn: findSeriesById', () => {
test('should find Series by Id', async () => {
const series = new Series();
MainListManager['mainList'].push(series);
const result = await MainListSearcher.findSeriesById(series.id);
expect(result?.id).toBe(series.id);
});

test('should return on no result', async () => {
const result = await MainListSearcher.findSeriesById('id');
expect(result).toBeNull();
});
});
```
(You can turn Series to a object with just a id for reproducing):
`{id:''}
`

JestConfig:
```
'use strict';

module.exports = {
'roots': [
''
],
'testMatch': [
'**/test/**/*-(test|tests).+(ts|tsx|js)',
],
'transform': {
'\\.(gql|graphql)$': '@jagi/jest-transform-graphql',
'^.+\\.(ts|tsx)$': 'ts-jest'
},
'reporters': [
'default',
['jest-html-reporters', {
'publicPath': './html-report',
'filename': 'report.html',
'expand': true
}]
],
'testTimeout': 3500,
'verbose': false,
'globals': {
'ts-jest': {
babelConfig: true,
}
},
'testEnvironment': 'node',
'setupFilesAfterEnv': ['/test/test-helper.ts'],
'maxWorkers': 6
};

```

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.