簡體   English   中英

如何防止 Prettier 將測試代碼從 1 行變為多行

[英]How to prevent Prettier from breaking test code from 1 line to multiple lines

有問題的代碼

it('will display No Policy Found after fist submit attempt.', () => {
    const policyDetails = {
        partyID: null,
        agreementID: null,
        isValidPolicy: false,
    };
    wrapper.setProps({policyDetails});
    wrapper.setState({submitCount: 1});
    const result = wrapper.instance().displayUserNotices();
    const render = shallow(result)
        .find('UserNotice')
        .find('p');

    expect(render.text()).toEqual(NO_POLICY_USER_NOTICE);
});

我繼續寫

const render = shallow(result)
  .find('UserNotice')
  .find('p');

作為所需的以下 1-liner:

const render = shallow(result).find('UserNotice').find('p');

但更漂亮的一直在恢復它。

我嘗試添加

noUnexpectedMultiline: true in the.prettierrc.yml 但這不起作用。

想法?

要防止 Prettier 格式化您的代碼,請在變量/函數/等之前使用此注釋。

// prettier-ignore

這也適用於多行。 例如

// prettier-ignore-start
const foo      =         'hey';
console.log      (foo);
// prettier-ignore-end

更多信息: https://prettier.io/docs/en/ignore.html

如果這是在您的代碼庫中經常發生的事情,並且您不想在代碼中使用ignore語句,並且希望啟用超過 80 個字符(Prettier 建議要這樣做),您可以將寬度增加添加

"printWidth": <whatever you want your max column length to be>

給你prettierrc.json的rc.json

文檔中的打印寬度

或者,如果您不想要代碼中的注釋,您可以創建一個.prettierignore 文件並將您的文件添加到該文件中。

希望看到他們在 Javascript 中添加忽略特定功能的能力,例如“console.log”。 不得不更改我的代碼片段以在每行的末尾添加 //prettier-ignore。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM