cse110-sp23-group3 / 8-ball

0 stars 0 forks source link

Will we use unit test on the project? #1

Closed phuanh004 closed 1 year ago

phuanh004 commented 1 year ago

Possible unittest:

nsyousef commented 1 year ago

I asked ChatGPT about how to write unit tests without using an external framework. Here is what it said:

Screenshot 2023-04-22 at 9 53 06 PM

phuanh004 commented 1 year ago

I asked ChatGPT about how to write unit tests without using an external framework. Here is what it said:

Screenshot 2023-04-22 at 9 53 06 PM

That's cool. But make sure you guys have nodejs on ur laptop/ pc.

nsyousef commented 1 year ago

I think we need to install node.js for Lab 4 anyways.

nsyousef commented 1 year ago

More on unit tests: https://stackoverflow.com/questions/58242163/how-to-test-javascript-without-a-framework

phuanh004 commented 1 year ago

The example above could be done using GitHub action.

// Create a file named test.js
// Define a simple add function
function add(a, b) {
  return a + b;
}

// Define a simple test function
function test(name, fn) {
  try {
    fn();
    console.log(`✅ ${name}`);
  } catch (error) {
    console.error(`❌ ${name}`);
    console.error(error);
    process.exitCode = 1;
  }
}

// Write a test case for the add function
test('add(1, 2) should return 3', () => {
  const actual = add(1, 2);
  const expected = 3;
  if (actual !== expected) {
    throw new Error(`Expected ${expected}, but got ${actual}`);
  }
});

This code snippet is adapted from https://github.com/remarkablemark/github-actions-nodejs/blob/master/test.js. You can run this test with GitHub actions by creating a file named .github/workflows/test.yml with the following content:

name: Test
on: [push]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: Install dependencies
        run: npm install
      - name: Run tests
        run: node test.js