Skip to content

Instantly share code, notes, and snippets.

@kohki-shikata
Created July 8, 2024 03:03
Show Gist options
  • Save kohki-shikata/690cfdfc2ef9b9dcc1775c07c0b09328 to your computer and use it in GitHub Desktop.
Save kohki-shikata/690cfdfc2ef9b9dcc1775c07c0b09328 to your computer and use it in GitHub Desktop.
[Node.js]ランダムな整数を出力する(テスト)
// テストにはJestを使用
import { createRandom } from "../functions/helpers";
describe('Create random number between two value: Normal Pattern', () => {
test('Return random value is correct', () => {
const min = 1;
const max = 5;
expect(createRandom(min, max)).toBeLessThanOrEqual(max)
expect(createRandom(min, max)).toBeGreaterThanOrEqual(min)
})
})
describe('Create random number between two value: Negative Pattern', () => {
test("Not be able to input zero value for minimum value", () => {
const min = 0;
const max = 5;
try {
createRandom(min, max);
} catch (error: any) {
expect(() => createRandom(min, max)).toThrow(error);
}
});
test("Not be able to input zero value for maximum value", () => {
const min = 1;
const max = 0;
try {
createRandom(min, max);
} catch (error: any) {
expect(() => createRandom(min, max)).toThrow(error);
}
});
test("Not be able to input negative value for minimum value", () => {
const min = -100;
const max = 5;
try {
createRandom(min, max);
} catch (error: any) {
expect(() => createRandom(min, max)).toThrow(error);
}
});
test("Not be able to input negative value for maximum value", () => {
const min = 1;
const max = -200;
try {
createRandom(min, max);
} catch (error: any) {
expect(() => createRandom(min, max)).toThrow(error);
}
});
test("Not be able to input float value for minimum value", () => {
const min = 1.4;
const max = 5;
try {
createRandom(min, max);
} catch (error: any) {
expect(() => createRandom(min, max)).toThrow(error);
}
});
test("Not be able to input float value for maximum value", () => {
const min = 1;
const max = 5.291;
try {
createRandom(min, max);
} catch (error: any) {
expect(() => createRandom(min, max)).toThrow(error);
}
});
test("Not be able to input value for minimum value greater than maximum value", () => {
const min = 1024;
const max = 256;
try {
createRandom(min, max);
} catch (error: any) {
expect(() => createRandom(min, max)).toThrow(error);
}
});
test("Not be able to input value as string", () => {
const min = "minimum";
const max = "maximum";
try {
createRandom(min as any, max as any);
} catch (error: any) {
expect(() => createRandom(min as any, max as any)).toThrow(error);
}
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment