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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README to show more options for disabling and overrides #165

Merged
merged 1 commit into from Mar 25, 2024

Conversation

ADTC
Copy link
Contributor

@ADTC ADTC commented Mar 22, 2024

The new feature of disabling with empty array can do more than just disabling globally and enabling for certain folders or files. It can do the opposite: enabling globally and disabling for certain folders or files. It can also do sort order overrides, where a different order applies to certain folders and files instead of the global order.

Feel free to suggest a rewrite of the text if needed. 馃檪 I have used the reverse configuration in an actual project and it works well:

// .prettierrc
{
  "plugins": ["@ianvs/prettier-plugin-sort-imports"],
  "overrides": [
    {
      "files": "please/doNot/sortThis.ts",
      "options": {
        "importOrder": [] // disabled
      }
    }
  ],
  "importOrder": [ /* global order here */ ]
  //...
}

Tip: The overrides order can come before or after the global importOrder. It doesn't matter as Prettier will always apply the override regardless of where it is in .prettierrc.

The new feature of disabling with empty array can do more than just disabling globally and enabling for certain folders or files. It can do the opposite: enabling globally and disabling for certain folders or file. It can also do sort order overrides, where a different order applies to certain folders and files instead of the global order.
@ADTC
Copy link
Contributor Author

ADTC commented Mar 22, 2024

BTW: Would specifying "importOrder": null inside the overrides config make it use the default order (ignoring the custom global order)?

@IanVS
Copy link
Owner

IanVS commented Mar 25, 2024

Thanks for the contribution.

And yes, null is treated the same as undefined by prettier, and will cause the default to be used.

@IanVS IanVS merged commit 135b6e5 into IanVS:main Mar 25, 2024
8 checks passed
@ADTC ADTC deleted the patch-1 branch March 25, 2024 13:54
@farzadmf
Copy link

farzadmf commented May 2, 2024

@ADTC question about this:

I have the following config:

{
  "endOfLine": "auto",
  "importOrder": ["^@/(.*)$", "^[./]"],
  "importOrderParserPlugins": ["typescript", "jsx", "decorators-legacy"],
  "importOrderSeparation": true,
  "importOrderSortSpecifiers": true,
  "plugins": ["@ianvs/prettier-plugin-sort-imports"],
  "overrides": [
    {
      "files": "**/*.spec.ts*",
      "options": {
        "importOrder": []
      }
    }
  ],
  "printWidth": 120,
  "proseWrap": "always",
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "all"
}

And I have a test file named ...spec.tsx like this:

const myMock = jest.fn();

jest.mock('src/dependency/used/by/MyModule', () => { /* ... */ });

// import stuff
// import MyModule (jest.mock above would capture the dependency imported by this guy)

it('my tests', () => { /* ... */ });

Now, even with the override, the jest calls are moved after the imports, preventing the dependency to be mocked. Is there something I'm missing in the config?

@fbartho
Copy link
Collaborator

fbartho commented May 2, 2024

@farzadmf this plugin is set to hoist import expressions up to the top of the file. You probably want to disable this plugin on files that use jest.mock in that way since that鈥檚 intrinsically incompatible with any tooling that reorganizes imports and code.

@IanVS
Copy link
Owner

IanVS commented May 3, 2024

I believe jest.mock hoists itself to the top no matter where it is when the file is run. That's because in general, imports are always evaluated first even if other code is above it.

Maybe you can open a new issue about the exclusion pattern not working?

@farzadmf
Copy link

farzadmf commented May 3, 2024

Thank you both @IanVS and @fbartho for your replies.

You probably want to disable this plugin on files that use jest.mock in that way

That's what I'm trying to do actually 馃槅 . Although I'm pretty sure it's something I'm missing because I'd be surprised if people using jest (and its mocking) aren't sorting their imports (please see below for more context, in case you can think of something)

Maybe you can open a new issue about the exclusion pattern not working?

I can do that for sure; wanted to first confirm if I'm doing it properly or not, and what I expect to happen is what should happen

I believe jest.mock hoists itself to the ...

You lost me on this one 馃槤 , you said:

  • jest.mock hoists itself to the top
  • imports are always evaluated first

So, then, what's happening here? jest.mock is first or the imports 馃

That being said, I must say I'm not super familiar with jest mocking, so if you guys know a better way, please do let me know (more context below).


Here's my situation:

  • I have src/a.tsx, src/b.ts, where b defines an object like this:
    export const myObj = {
      hello: ...
    }
  • I'm testing a, which does import { myObj } from b
  • I want to mock b, so in a.spec.tsx, there's:
    const myMock = jest.fn();
    
    jest.mock('src/b', () => ({
      myObj: {
        hello: myMock,
      }
    }))
    
    import { blah } from 'src/a';
    
    it('test blah with the mock etc', () => { /* ... */ })

Please let me know if there's a better way to do this so that I can keep the import sorting

@IanVS
Copy link
Owner

IanVS commented May 3, 2024

This isn't really the best place to debug jest issues. I'd suggest reading https://jestjs.io/docs/manual-mocks#using-with-es-module-imports, and if you're trying to run jest in experimental ESModule mode, then it seems not to be supported and you might want to consider migrating to vitest instead, which has much better ESM support.

Bottom line, is that sorting does not impact jest.mock (or vi.mock), so you can continue sorting imports without any impact on your test functionality.

@farzadmf
Copy link

farzadmf commented May 3, 2024

This isn't really the best place to debug jest issues

You're absolutely right; sorry about that; a bit of a desparate measure 馃檪

you might want to consider migrating to vitest instead

Was honestly debating about that; now that you mentioned it, I will check it out


And, just in case of a 0.0001% chance that someone might face the same issue as me:

  • I ended up doing a dynamic import of my module inside my it tests.
  • So, in my it block, I do const { blah } = await import('src/a').

This way, the imports can be sorted, and the tests (and mockcing) work properly.

Thank you again @IanVS for the help and the great plugin

@ADTC
Copy link
Contributor Author

ADTC commented May 3, 2024

I apologize that I'm not of much help here. I think this discussion is better moved to a separate issue, rather than in this PR. Thank you though. 馃檪

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

Successfully merging this pull request may close these issues.

None yet

4 participants