首页 > 解决方案 > expect(received).toEqual(expected) // npm 测试中的深度相等,更改行时的字符串添加一个“.” 最后在返回的字符串中

问题描述

我正在使用 python 3 为待办事项列表制作一个程序,它有一个帮助功能,可以在以下控制台上打印:

Usage:-
    $ ./todo add "todo item"  # Add a new todo
    $ ./todo ls               # Show remaining todos
    $ ./todo del NUMBER       # Delete a todo
    $ ./todo done NUMBER      # Complete a todo
    $ ./todo help             # Show usage
    $ ./todo report           # Statistics

当我在它上面运行 npm test 时。测试用例显示以下消息:

FAIL ./todo.test.js (13.527 s) × prints help when no additional args are provided (277 ms) × prints help (238 ms) √ add a single todo (237 ms) √ show error message when add is not followed by a todo (237 ms) √ add multiple todos (750 ms) × list todos in reverse order (added latest first) (1000 ms) √ list when there are no remaining todos (230 ms) √ delete a todo (972 ms) √ delete todos numbered 3, 2 & 1 (1426 ms) √ delete first todo item 3 times (1438 ms) √ delete non-existent todos (1440 ms) √ delete does not have enough arguments (251 ms) √ mark a todo as done (956 ms) √ mark as done a todo which does not exist (953 ms) √ mark as done without providing a todo number (945 ms) × report pending & completed todos (1466 ms)

● prints help when no additional args are provided

expect(received).toEqual(expected) // deep equality

Expected: StringContaining "Usage :-
$ ./todo add \"todo item\"  # Add a new todo
$ ./todo ls               # Show remaining todos
$ ./todo del NUMBER       # Delete a todo
$ ./todo done NUMBER      # Complete a todo
$ ./todo help             # Show usage
$ ./todo report           # Statistics"
Received: "Usage:-·
    $ ./todo add \"todo item\"  # Add a new todo·
    $ ./todo ls               # Show remaining todos·
    $ ./todo del NUMBER       # Delete a todo·
    $ ./todo done NUMBER      # Complete a todo·
    $ ./todo help             # Show usage·
    $ ./todo report           # Statistics·
"

  26 |   let received = execSync(todoTxtCli()).toString("utf8");
  27 |
> 28 |   expect(received).toEqual(expect.stringContaining(usage));
     |                    ^
  29 | });
  30 |
  31 | test("prints help", () => {

  at Object.<anonymous> (todo.test.js:28:20)
● prints help

expect(received).toEqual(expected) // deep equality

Expected: StringContaining "Usage :-
$ ./todo add \"todo item\"  # Add a new todo
$ ./todo ls               # Show remaining todos
$ ./todo del NUMBER       # Delete a todo
$ ./todo done NUMBER      # Complete a todo
$ ./todo help             # Show usage
$ ./todo report           # Statistics"
Received: "Usage:-·
    $ ./todo add \"todo item\"  # Add a new todo·
    $ ./todo ls               # Show remaining todos·
    $ ./todo del NUMBER       # Delete a todo·
    $ ./todo done NUMBER      # Complete a todo·
    $ ./todo help             # Show usage·
    $ ./todo report           # Statistics·
"

  32 |   let received = execSync(todoTxtCli("help")).toString("utf8");
  33 |
> 34 |   expect(received).toEqual(expect.stringContaining(usage));
     |                    ^
  35 | });
  36 |
  37 | test("add a single todo", () => {

  at Object.<anonymous> (todo.test.js:34:20)
● list todos in reverse order (added latest first)

expect(received).toEqual(expected) // deep equality

Expected: StringContaining "[3] find needle in the haystack
[2] water the plants
[1] the thing i need to do
"
Received: "[3] find needle in the haystack·
[2] water the plants·
[1] the thing i need to do·
"

  80 |   let received = execSync(todoTxtCli("ls")).toString("utf8");
  81 |
> 82 |   expect(received).toEqual(expect.stringContaining(expected));
     |                    ^
  83 | });
  84 |
  85 | test("list when there are no remaining todos", () => {

  at Object.<anonymous> (todo.test.js:82:20)
● report pending & completed todos

expect(received).toEqual(expected) // deep equality

Expected: StringContaining "2020-12-22 Pending : 1 Completed : 2"
Received: "2020-12-23 Pending : 1 Completed : 2·
"

  216 |   let received = execSync(todoTxtCli("report")).toString("utf8");
  217 |
> 218 |   expect(received).toEqual(expect.stringContaining(expected));
      |                    ^
  219 | });
  220 |

  at Object.<anonymous> (todo.test.js:218:20)
Test Suites: 1 failed, 1 total Tests: 4 failed, 12 passed, 16 total Snapshots: 0 total Time: 14.546 s Ran all test suites. npm ERR! code 1 npm ERR! path C:\Users\shubh\Desktop\TODO\python npm ERR! command failed npm ERR! command C:\Windows\system32\cmd.exe /d /s /c jest

npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\shubh\AppData\Local\npm-cache_logs\2020-12-22T19_06_10_509Z-debug.log

我无法理解为什么有一个“。” 在每行末尾添加符号。这是与预期输出相同的控制台输出。控制台输出

请帮我解决它。这是帮助 fxn 的代码:(语言:python 3)

   def help():
    print('''Usage:-
    $ ./todo add "todo item"  # Add a new todo
    $ ./todo ls               # Show remaining todos
    $ ./todo del NUMBER       # Delete a todo
    $ ./todo done NUMBER      # Complete a todo
    $ ./todo help             # Show usage
    $ ./todo report           # Statistics''')

我也尝试过使用不同的打印语句,但没有运气/下面是 todo.test.js 文件代码:

const fs = require("fs");
const { execSync } = require("child_process");

let deleteFile = (path) => {
  try {
    fs.unlinkSync(path);
  } catch (err) {}
};

beforeEach(() => {
  deleteFile(`${__dirname}/todo.txt`);
  deleteFile(`${__dirname}/done.txt`);
});

let todoTxtCli = (...args) => [`${__dirname}/todo`, ...args].join(" ");

let usage = `Usage :-
$ ./todo add "todo item"  # Add a new todo
$ ./todo ls               # Show remaining todos
$ ./todo del NUMBER       # Delete a todo
$ ./todo done NUMBER      # Complete a todo
$ ./todo help             # Show usage
$ ./todo report           # Statistics`;

test("prints help when no additional args are provided", () => {
  let received = execSync(todoTxtCli()).toString("utf8");

  expect(received).toEqual(expect.stringContaining(usage));
});

test("prints help", () => {
  let received = execSync(todoTxtCli("help")).toString("utf8");

  expect(received).toEqual(expect.stringContaining(usage));
});

test("add a single todo", () => {
  let expected = 'Added todo: "the thing i need to do"';
  let received = execSync(
    todoTxtCli("add", '"the thing i need to do"')
  ).toString("utf8");

  expect(received).toEqual(expect.stringContaining(expected));
});

test("show error message when add is not followed by a todo", () => {
  let expected = "Error: Missing todo string. Nothing added!";
  let received = execSync(todoTxtCli("add")).toString("utf8");

  expect(received).toEqual(expect.stringContaining(expected));
});

test("add multiple todos", () => {
  let todos = [
    "the thing i need to do",
    "water the plants",
    "find needle in the haystack",
  ];

  todos.forEach((todo, i) => {
    let expected = `Added todo: "${todo}"`;
    let received = execSync(todoTxtCli("add", `"${todo}"`)).toString("utf8");

    expect(received).toEqual(expect.stringContaining(expected));
  });
});

test("list todos in reverse order (added latest first)", () => {
  let todos = [
    "the thing i need to do",
    "water the plants",
    "find needle in the haystack",
  ];
  todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));

  let expected = `[3] find needle in the haystack
[2] water the plants
[1] the thing i need to do
`;
  let received = execSync(todoTxtCli("ls")).toString("utf8");

  expect(received).toEqual(expect.stringContaining(expected));
});

test("list when there are no remaining todos", () => {
  let expected = `There are no pending todos!`;
  let received = execSync(todoTxtCli("ls")).toString("utf8");

  expect(received).toEqual(expect.stringContaining(expected));
});

test("delete a todo", () => {
  let todos = [
    "the thing i need to do",
    "water the plants",
    "find needle in the haystack",
  ];
  todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));

  let expected = "Deleted todo #2";
  let received = execSync(todoTxtCli("del", "2")).toString("utf8");

  expect(received).toEqual(expect.stringContaining(expected));
});

test("delete todos numbered 3, 2 & 1", () => {
  let todos = [
    "the thing i need to do",
    "water the plants",
    "find needle in the haystack",
  ];
  todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));

  [3, 2, 1].forEach((n) => {
    let expected = `Deleted todo #${n}`;
    let received = execSync(todoTxtCli("del", n.toString())).toString("utf8");

    expect(received).toEqual(expect.stringContaining(expected));
  });
});

test("delete first todo item 3 times", () => {
  let todos = [
    "the thing i need to do",
    "water the plants",
    "find needle in the haystack",
  ];
  todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));

  [1, 1, 1].forEach((n) => {
    let expected = `Deleted todo #${n}`;
    let received = execSync(todoTxtCli("del", n.toString())).toString("utf8");

    expect(received).toEqual(expect.stringContaining(expected));
  });
});

test("delete non-existent todos", () => {
  let todos = [
    "the thing i need to do",
    "water the plants",
    "find needle in the haystack",
  ];
  todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));

  [0, 4, 5].forEach((n) => {
    let expected = `Error: todo #${n} does not exist. Nothing deleted.`;
    let received = execSync(todoTxtCli("del", n.toString())).toString("utf8");

    expect(received).toEqual(expect.stringContaining(expected));
  });
});

test("delete does not have enough arguments", () => {
  let expected = "Error: Missing NUMBER for deleting todo.";
  let received = execSync(todoTxtCli("del")).toString("utf8");

  expect(received).toEqual(expect.stringContaining(expected));
});

test("mark a todo as done", () => {
  let todos = [
    "the thing i need to do",
    "water the plants",
    "find needle in the haystack",
  ];
  todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));

  let expected = "Marked todo #2 as done.";
  let received = execSync(todoTxtCli("done", "2")).toString("utf8");

  expect(received).toEqual(expect.stringContaining(expected));
});

test("mark as done a todo which does not exist", () => {
  let todos = [
    "the thing i need to do",
    "water the plants",
    "find needle in the haystack",
  ];
  todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));

  let expected = "Error: todo #0 does not exist.";
  let received = execSync(todoTxtCli("done", "0")).toString("utf8");

  expect(received).toEqual(expect.stringContaining(expected));
});

test("mark as done without providing a todo number", () => {
  let todos = [
    "the thing i need to do",
    "water the plants",
    "find needle in the haystack",
  ];
  todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));

  let expected = "Error: Missing NUMBER for marking todo as done.";
  let received = execSync(todoTxtCli("done")).toString("utf8");

  expect(received).toEqual(expect.stringContaining(expected));
});

test("report pending & completed todos", () => {
  let todos = [
    "the thing i need to do",
    "water the plants",
    "find needle in the haystack",
  ];
  todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));

  execSync(todoTxtCli("done", "1"));
  execSync(todoTxtCli("done", "2"));

  let date = new Date();
  let expected = `${date.toISOString().slice(0, 10)} Pending : 1 Completed : 2`;
  let received = execSync(todoTxtCli("report")).toString("utf8");

  expect(received).toEqual(expect.stringContaining(expected));
});

标签: javascriptpythonnode.jsnpmgulp-npm-test

解决方案


推荐阅读