Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to import modules inside imported module from tests #137

Closed
wilomgfx opened this issue Mar 23, 2017 · 5 comments
Closed

Fails to import modules inside imported module from tests #137

wilomgfx opened this issue Mar 23, 2017 · 5 comments

Comments

@wilomgfx
Copy link

  • Issue
    image
    When ever i link a module from my typescript source files, it seems like the modules inside that said imported module are not imported.

Heres an example test

import { User } from './../src/types/models';
import Api from '../src/api';
import { t } from '../src/configs/i18n'

const api = new Api()

// beforeEach()

describe('Api client', () => {

  it('Should return wilomgx as a user', (done) => {
    const login = api.getUserByLogin({
      user:'wilomgfx',
      pw:'wilomgfxis2op4you'
    })
    login.then(({data} : {data: User}) => {
      expect(data).toBeInstanceOf(User)
      expect(data.fullName).toBe('William Graphics')
      done()
    })
    .catch(e => done(e))
  })
 //...more tests
}

My config

"jest": {
    "preset": "react-native",
    "transform": {
      "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
      ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "(/test/.*|\\.(test|spec))\\.(ts|tsx|js)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ],
    "testResultsProcessor": "<rootDir>/node_modules/ts-jest/coverageprocessor.js"

To make it work i have to import my compiled javascript from my build folder

import { User } from './../build/types/models';
import Api from '../build/api';
import { t } from '../build/configs/i18n'

const api = new Api()

// beforeEach()

describe('Api client', () => {

  it('Should return wilomgx as a user', (done) => {
    const login = api.getUserByLogin({
      user:'wilomgfx',
      pw:'wilomgfxis2op4you'
    })
    login.then(({data} : {data: User}) => {
      expect(data).toBeInstanceOf(User)
      expect(data.fullName).toBe('William Graphics')
      done()
    })
    .catch(e => done(e))
  })
 //...more tests
}

Heres the imported file that is causing the error when referencing the typescript file from the src folder

import I18n from 'react-native-i18n'
// Enable fallbacks if you want `en-US` and `en-GB` to fallback to `en`
I18n.fallbacks = true

//code to set translations, not relevant here
  • Expected behavior

Work ? I dont know, maybe my configs are wrong... but it wont work for required typescript files.
Tried to use an other file with some other random imported file, and it wont work either.

@wilomgfx wilomgfx changed the title Fails to import modules inside called modules from tests Fails to import modules inside imported module from tests Mar 23, 2017
@kulshekhar
Copy link
Owner

please create a minimal repo which reproduces this issue

@wilomgfx
Copy link
Author

wilomgfx commented Mar 24, 2017

There you go : https://github.com/wilomgfx/ts-jest-import-issue-repo

As soon as i import a module from npm (in this case react-native-i18n just like in my real app) it won't import it properly causing an undefined error.

@kulshekhar
Copy link
Owner

@wilomgfx the import is incorrect. Use

import * as I18n from 'react-native-i18n';

I've sent a PR to your repo.

@wilomgfx
Copy link
Author

wilomgfx commented Mar 24, 2017

Oh waow, thanks! Indeed. Kinda strange that it worked even if i was not importing everything from the said package when building up for the app. Thanks a lot :P

Any idea why ?

@aaronjensen
Copy link

In case anyone else comes here and ends up seeing

error TS2497: Module '"X"' resolves to a non-module entity and cannot be imported using this construct.

Check out microsoft/TypeScript#5073

tl;dr add declare namespace X {} to your X.d.ts to work around this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants