andywer / andywer/threads.js

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

Aperta
#269 15 commenti 1 reazione 0 assegnatari Vedi su GitHub
question
Lingua principale
TypeScript
Stelle
3.5k
Fork
173
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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
};

```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.