assert 모듈은 기능 주장에 사용되는 다양한 기능을 제공합니다. Assert.notDeepEqual()은 실제 매개변수와 예상 매개변수 간의 불평등을 확인합니다. 또한 매개변수는 완전히 동일하지 않아야 합니다. 조건이 충족되지 않으면 오류가 발생합니다.
구문
assert.notDeepEqual(actual, expected[, message])
매개변수
위의 매개변수는 다음과 같이 설명됩니다. -
-
실제 – 이 매개변수는 비교해야 하는 실제 값을 보유합니다.
-
예상 – 이것은 확인해야 하는 예상 매개변수 값을 보유합니다.
-
메시지 – 이것은 선택적 매개변수입니다. 함수가 실행되어 에러가 발생했을 때 출력되는 사용자 정의 메시지입니다.
Assert 모듈 설치
npm install assert
assert 모듈은 내장된 Node.js 모듈이므로 이 단계도 건너뛸 수 있습니다. 최신 assert 모듈을 얻으려면 다음 명령을 사용하여 assert 버전을 확인할 수 있습니다.
npm version assert
함수에서 모듈 가져오기
const assert = require("assert").strict; 예시
이름이 assertNotDeepEqual.js인 파일을 만들고 아래 코드 조각을 복사합니다. 파일을 생성한 후 아래 명령을 사용하여 이 코드를 실행하십시오.
node assertNotDeepEqual.js
assertNotDeepEqual.js
// Importing the module
const assert = require('assert').strict;
try {
// Both the values should not be identical
assert.notDeepEqual({ a: '21' }, { a: '21' });
} catch(error) {
console.log("Error: ", error)
} 출력
C:\home\node>> node assertNotDeepEqual.js
Error: { AssertionError [ERR_ASSERTION]: Identical input passed to
notDeepStrictEqual:
{
a: '21'
}
at Object.<anonymous> (/home/mayankaggarwal/mysql-test/assert.js:6:9)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
generatedMessage: true,
name: 'AssertionError [ERR_ASSERTION]',
code: 'ERR_ASSERTION',
actual: { a: '21' },
expected: { a: '21' },
operator: 'notDeepStrictEqual' } 예시
예를 하나 더 살펴보겠습니다.
// Importing the module
const assert = require('assert').strict;
try {
// Both the values should not be identical
assert.notDeepEqual({ a: 21 }, { a: '21' });
console.log("Values are not identical")
} catch(error) {
console.log("Error: ", error)
} 출력
C:\home\node>> node assertNotDeepEqual.js Values are not identical
위의 예에서 한 값은 문자열 유형이고 다른 값은 정수 유형이므로 같지 않다는 것을 알 수 있습니다.