typeorm-factory icon indicating copy to clipboard operation
typeorm-factory copied to clipboard

SyntaxError: Unexpected token 'export'

Open adinanp opened this issue 1 year ago • 5 comments

When trying to recreate the examples from README, I get a type error.

Facatory:

import {
  CollectionSubfactory,
  Factory,
  LazyInstanceAttribute,
  FactorizedAttrs,
} from '@jorgebodega/typeorm-factory';
import { Document } from '../../../src/modules/documents/entities/document.entity';
import { faker } from '@faker-js/faker';
import { ContentType, Status } from '../../../src/modules/common/enums';
import { TagFactory } from '../commons/tags.factory';
import dataSource from '../../../src/modules/configuration/database/datasource';

export class DocumentFactory extends Factory<Document> {
  protected entity = Document;

  protected dataSource = dataSource;

  protected attrs(): FactorizedAttrs<Document> {
    return {
      title: faker.string.sample,
      thumbnail: faker.string.uuid,
      file: faker.string.uuid,
      slug: faker.string.sample,
      description: faker.string.sample,
      contentType: ContentType.ARTICLE,
      status: Status.ARCHIVED,
      tags: new LazyInstanceAttribute(
        () => new CollectionSubfactory(TagFactory, 1),
      ),
    };
  }
}

The error

SyntaxError: Unexpected token 'export'

Full stack trace (expand) ```
    ● Test suite failed to run
  
      Jest encountered an unexpected token
  
      Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
  
      Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
  
      By default "node_modules" folder is ignored by transformers.
  
      Here's what you can do:
       • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
       • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
       • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
       • If you need a custom transformation specify a "transform" option in your config.
       • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
  
      You'll find more details and examples of these config options in the docs:
      https://jestjs.io/docs/configuration
      For information about custom transformations, see:
      https://jestjs.io/docs/code-transformation
  
      Details:
  
      /Users/adinan.paiva/projects/sanar/learning-api/node_modules/@jorgebodega/typeorm-factory/dist/index.js:1
      ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from './factory';
                                                                                        ^^^^^^
  
      SyntaxError: Unexpected token 'export'
  
      > 1 | import {
          | ^
        2 |   CollectionSubfactory,
        3 |   Factory,
        4 |   LazyInstanceAttribute,
  
        at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1796:14)
        at Object.<anonymous> (../test/factories/assets/document.factory.ts:1:1)
```

Versions:

├── [email protected]
├── [email protected]
├── @jorgebodega/[email protected]
├── @nestjs/[email protected]

This is a bug or a miss configuration?

Let me see if you need more reproducible information to help me.

Thanks

adinanp avatar Apr 01 '24 18:04 adinanp

Having the same. @adinanp did you find a work around?

giorgiofellipe avatar Apr 03 '24 20:04 giorgiofellipe

Can you provide a reproducible example? I will take a look in the following days.

But, I must say, this looks like a problem with node esm vs commonjs.

jorgebodega avatar Apr 03 '24 21:04 jorgebodega

I've fixed by adding these to my jest.config:

  globals: {
    'ts-jest': {
      tsconfig: {
        allowJs: true,
      },
    },
  },
  extensionsToTreatAsEsm: ['.ts'],
  transform: {
    '^.+\\.[tj]s$': 'ts-jest',
  },
  transformIgnorePatterns: ['/node_modules/(?!@jorgebodega/typeorm-factory)'],

giorgiofellipe avatar Apr 05 '24 16:04 giorgiofellipe

I confirm that the same error happened to me when upgrading to version 2.0.0

BenStirrup avatar Jun 06 '24 15:06 BenStirrup

Same problem here. We are using mocha in a type: module package.

tillkolter avatar Jul 26 '24 11:07 tillkolter