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

Add isMacrosName option #133

Merged
merged 2 commits into from
Nov 20, 2019
Merged

Add isMacrosName option #133

merged 2 commits into from
Nov 20, 2019

Conversation

futagoza
Copy link
Contributor

@futagoza futagoza commented Nov 19, 2019

What: Resolving #131 by adding a new option called isMacrosName for the plugin.

Why: Allows the user to selectively accept modules as a macro for this plugin.

How: This option will optionally overwrite the original behaviour, so backwards compatibility is kept.

@futagoza
Copy link
Contributor Author

futagoza commented Nov 19, 2019

@kentcdodds I tried adding tests, and they were passing, but for some reason, the test macros › supports macros from node_modules broke. Tried removing the tests (including the new fixture I added) and tried to run the tests again to check if that fixed it, but the runner refuses to run because there have been no changes since the last commit, what should I do?

EDIT: Just for reference, here are the tests and fixture I tried adding:

// Tests, added to: src/__tests__/index.js, after Line 412

    {
      title: 'when a custom isMacrosName option is used on a import',
      pluginOptions: {
        isMacrosName(v) {
          return v.endsWith('-macro.js')
        },
      },
      code: `
        import myEval from './fixtures/eval-macro.js'
        const x = myEval\`34 + 45\`
      `,
    },
    {
      title: 'when a custom isMacrosName option is used on a require',
      pluginOptions: {
        isMacrosName(v) {
          return v.endsWith('-macro.js')
        },
      },
      code: `
        const evaler = require('./fixtures/eval-macro.js')
        const x = evaler\`34 + 45\`
      `,
    },

// Fixture: src/__tests__/fixtures/eval-macro.js

    module.exports = require('./eval.macro.js')

@kentcdodds
Copy link
Owner

Thanks!

The terminal is interactive with Jest watch mode. Read the output and press the keys that make sense :)

@futagoza
Copy link
Contributor Author

Without the new tests and fixture:

  1. I run npm run test
  2. It refuses to run because there have been no changes, and gives me options
  3. I press a (which it says runs all tests)
  4. It refuses to run because there have been no changes, and tells me to press w to see options
  5. I press w, and it shows the options again but a has disappeared, so I try every other option
  6. I see 4 again.... and again

When I add the new tests and fixture it passes these tests no problem, but macros › supports macros from node_modules breaks (an error says it expected the function to be called once, but it wasn't), but because I can't run the tests without new uncommitted tests, I can't tell if the problem is:

  • the new uncommitted tests
  • the actual test that is failing
  • jest watch mode
  • something else (maybe another subtle windows compatibility issue 🤔)
  • my own stupidity (this is highly likely 🤣)

Is there any other way to force Jest to run the tests via kcd-scripts? While writing this it just occurred to me that I could try to create a new config and run the tests using npx jest --config new-config.js (don't know why I didn't think of it before when I was working on the fork a few hours ago, oh well). Looked at your jest config, but it looks like it's geared towards watch mode. Any tips that I can use when I get around to trying this later?

@kentcdodds
Copy link
Owner

That's odd. It should definitely run all the tests when you hit a.

You can force it to run the tests with npm run test -- --coverage and that'll run them all once and give you a coverage report.

Sorry about the trouble.

@futagoza
Copy link
Contributor Author

futagoza commented Nov 20, 2019

lol A bit late for that, I created a new jest.config.js (same as the config from kcd-scripts but without all the watch mode options and normalized, see below) and ran npx jest and that confirmed everything was working, so it's an issue with Jest's watch mode (and probably a subtle windows only bug). To commit it though, I had to change the script test in package.json to an empty string before the husky plugin allowed it through 😅

Just to confirm, before posting this I used npm run test -- --coverage and it also works.

// jest.config.js

const path = require('path')

const ignores = [
  '/node_modules/',
  '/fixtures/',
  '/__tests__/helpers/',
  '__mocks__',
]

module.exports = {
  roots: [path.join(__dirname, 'src')],
  testEnvironment: 'node',
  testURL: 'http://localhost',
  moduleFileExtensions: ['js', 'jsx', 'json', 'ts', 'tsx'],
  collectCoverageFrom: ['src/**/*.+(js|jsx|ts|tsx)'],
  testMatch: ['**/__tests__/**/*.+(js|jsx|ts|tsx)'],
  testPathIgnorePatterns: [...ignores],
  coveragePathIgnorePatterns: [...ignores, 'src/(umd|cjs|esm)-entry.js$'],
  transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'],
  coverageThreshold: {
    global: {
      branches: 100,
      functions: 100,
      lines: 100,
      statements: 100,
    },
  },
}

Should I do anything else, or is this good to merge?

Also, as a side note note related to this PR directly, you might want to:

  1. Update the Add yourself as a contributor section in CONTRIBUTING.md since kcd-scripts doesn't have the contributors script anymore
  2. Either update the opt into git hooks section in CONTRIBUTING.md or actually make them optional, as they are auto-installed on setup, which means they are mandatory right now

Copy link
Owner

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent. Thank you!

@kentcdodds kentcdodds merged commit c2e2ec9 into kentcdodds:master Nov 20, 2019
@kentcdodds
Copy link
Owner

@all-contributors please add @futagoza for code and tests

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @futagoza! 🎉

@kentcdodds
Copy link
Owner

🎉 This PR is included in version 2.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@futagoza
Copy link
Contributor Author

I forgot to update the user docs 😅, should I refork and update it, or you gonna do that later yourself?

@kentcdodds
Copy link
Owner

If you could make another pull request that would be great. Thanks!

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

Successfully merging this pull request may close these issues.

2 participants