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

Window is not defined in browser code with Typescript #2304

Closed
vjau opened this issue Jan 4, 2020 · 2 comments
Closed

Window is not defined in browser code with Typescript #2304

vjau opened this issue Jan 4, 2020 · 2 comments

Comments

@vjau
Copy link

vjau commented Jan 4, 2020

While testing typescript code in browser, i get a window is not defined using window

Here is my configuration

module.exports = function (wallaby) {
  return {
    files: [
      '**/*.js',
      '**/*.ts',
      { pattern: '**/tests/**/*.js', ignore: true },
      { pattern: '**/tests/**/*.ts', ignore: true },
      { pattern: 'node_modules/**/*.*', ignore: true }
    ],

    tests: [
      '**/tests/**/*.js',
      '**/tests/**/*.ts',
      { pattern: 'node_modules/**/*.*', ignore: true }
    ],
    testFramework: 'mocha',
    env: {
          type: 'node'
        },
    compilers: {
      '**/*.js*': wallaby.compilers.babel()
    },
    preprocessors: {
            '**/*.js': file => require('@babel/core').transform(
              file.content,
              {sourceMap: true, compact: false, filename: file.path, babelrc: true})
          },
    debug: true
  };
};

Here is my babelrc

{
  "env" : {
    "test" : {
      "presets": ["@babel/env", "@babel/react"]
    }
  }
}

Here is my tsconfig.json

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es2018",
    "module": "esNext",
    "lib": ["esnext", "dom"],
    "allowJs": true,
    "checkJs": false,
    "jsx": "preserve",
    "incremental": true,
    "noEmit": true,

    /* Strict Type-Checking Options */
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,

    /* Additional Checks */
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": false,
    "noFallthroughCasesInSwitch": false,

    /* Module Resolution Options */
    "baseUrl": ".",
    "paths": {
      /* Support absolute /imports/* with a leading '/' */
      "/*": ["*"]
    },
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "types": ["node", "mocha"],
    "esModuleInterop": true,
    "preserveSymlinks": true
  },
  "exclude": [
    "./.meteor/**",
    "./packages/**"
  ]
}

Thank you

@smcenlly
Copy link
Member

smcenlly commented Jan 5, 2020

The configuration that you provided specifies to run your project using node, and not in the browser, which is why window is not defined. Can you run your tests using mocha without Wallaby? Perhaps you are using/configuring JSDOM somewhere?

You may update your configuration to run in the browser by changing your env.type setting from node to browser, however in this case you'll likely need to configure webpack as well.

From the tsconfig.json file that you created, it looks like your project is using meteor. We have an example Meteor app configuration that may be useful to you. The meteor configuration actually sets global.window and a few other global variables that are required for meteor tests to execute correctly.

If that doesn't help, please provide your Wallaby diagnostics report. The Wallaby Diagnostics report is a simple JSON text report that contains some basic information about your project that is required to start troubleshooting most of Wallaby issues.

To share the report:

  • start Wallaby in your project;
  • wait until it reports some results (finishes running tests);
  • run Copy Diagnostics Report command:
    • in VS Code, Atom, or Sublime editor execute Wallaby Copy Diagnostics Report command;
    • in JetBrains IDEs or VS use Wallaby Help | Wallaby.js | Copy Diagnostics Report menu item;
  • paste generated report from your clipboard below (feel free to review and edit the report if required).

Also, if you don't make progress, if possible please also describe how you created your project or else provide a sample repo of your project structure. This will allow us to provide you with a working wallaby configuration.

@smcenlly smcenlly closed this as completed Jan 5, 2020
@vjau
Copy link
Author

vjau commented Jan 5, 2020

Thank you for your help.
Adding this code to wallaby config did the trick :

setup(){
      global.window = { navigator: { userAgent: 'node.js' } };
    }

The problem with Meteor is that there is code both for the browser and for the server

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

2 participants