Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Morten Ramfjords Workflow PR #401

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9f3b5ac
initial push
Ramsnes Jan 24, 2024
81952fc
installed eslint w/ prettier plugin, configured .eslintrc, configured…
Ramsnes Jan 24, 2024
5d6837a
testing intentional error
Ramsnes Jan 25, 2024
cdbc837
Create main.yml
Ramsnes Jan 26, 2024
459ed73
Merge pull request #1 from Ramsnes/Ramsnes-action-job-01
Ramsnes Jan 26, 2024
39a6192
added action job
Ramsnes Jan 26, 2024
fdb20e2
added action
Ramsnes Jan 26, 2024
e1baef9
inst jest, adds jest to package.json
Ramsnes Jan 26, 2024
1a9dc85
added jest to .eslintrc.json and package.json
Ramsnes Jan 26, 2024
0a08a5b
0.0.2
Ramsnes Jan 26, 2024
bbf5912
added babel
Ramsnes Jan 26, 2024
bbd8ba8
removed login.test.js example. jest and babel finished configing
Ramsnes Jan 26, 2024
40ddca5
inst and config cypress on files .eslintrc, package.json
Ramsnes Jan 26, 2024
0825be2
0.0.3
Ramsnes Jan 26, 2024
c1b98a2
cypress config
Ramsnes Jan 27, 2024
5bfac0d
finished both tests
Ramsnes Jan 27, 2024
8349b33
finished Jest, added autotest action job, unfinished cypress
Ramsnes Jan 27, 2024
ee60385
Create autoUnitTesting.yml
Ramsnes Jan 27, 2024
41bfe5e
Merge pull request #2 from Ramsnes/automatic-unit-testing
Ramsnes Jan 27, 2024
8b5b9a3
added cypress login and logout
Ramsnes Jan 28, 2024
392539b
completes cypress tests
Ramsnes Jan 28, 2024
95edcd5
completes cypress tests
Ramsnes Jan 28, 2024
31b7208
added readme
Ramsnes Jan 28, 2024
c362bd7
Create e2e-test.yml
Ramsnes Jan 28, 2024
053b4e5
Merge pull request #7 from Ramsnes/Ramsnes-patch-1
Ramsnes Jan 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:prettier/recommended"
],
"overrides": [
{
"files": [
"**/*.test.js",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should have one override for each testing platform, instead of merging these together.

"**/*.cy.js"
],
"env": {
"jest": true,
"cypress/globals": true
},
"plugins": [
"prettier",
"jest",
"cypress"
],
"extends": [
"plugin:jest/recommended",
"plugin:cypress/recommended"
],
"rules": {
"jest/prefer-expect-assertions": "off",
"cypress/no-unnecessary-waiting": "off",
"no-unused-vars": "off"
}
}
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {}
}
Comment on lines +1 to +42

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The ESLint configuration looks good overall, but there's a minor issue. It's a best practice to always end your files with a newline. This is because some tools like cat, sed, awk, etc., might not recognize or have issues processing the last line if it doesn't end with a newline.

    "rules": {}
- 42: }
+ 42: }\n

Also, you've turned off the rule no-unused-vars in your test files (line 33). While this can sometimes be necessary in test files, be cautious as it could potentially hide bugs related to unused variables in your tests. Consider enabling it and using ignore comments (// eslint-disable-next-line no-unused-vars) on a case-by-case basis where necessary.

25 changes: 25 additions & 0 deletions .github/workflows/autoUnitTesting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Automated Unit Testing
on:
pull_request:
workflow_dispatch:

jobs:
run-unit-tests:
name: Run Unit Tests
runs-on: ubuntu-latest

steps:
- name: Checkout under $GITHUB_WORKSPACE
uses: actions/checkout@main

- name: Set up NodeJS
uses: actions/setup-node@v1
with:
node-version: 16

- name: Install all dependencies
run: npm install

- name: Run Tests
run: npm run test-unit
# new
16 changes: 16 additions & 0 deletions .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Automated E2E Testing
on:
pull_request:
workflow_dispatch:
jobs:
run-e2e-tests:
name: Run E2E Tests
runs-on: ubuntu-latest
steps:
- name: Checkout under $GITHUB_WORKSPACE
uses: actions/checkout@main

- name: run cypress tests with electron
uses: cypress-io/github-action@v4
with:
browser: electron
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
/dist
/dist
.eslintcache
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Social media client

## Frameworks, tools and plugins

### eslint

ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs.

### Prettier

Prettier is an opinionated code formatter that supports a lot of different programming languages, like: JavaScript. JSON. JSX. CSS.

### Husky

Husky empowers developers to automate pre-commit checks, enhancing code quality and productivity.

### Jest

Jest is a JavaScript testing framework is used for testing applications, including login and logout functionality in this application-case.

### Cypress

Cypress is an end-to-end testing framework for web applications. In the context of testing login and logout functionality.

## Installation and configuration

### Clone the repository

```
git clone 'repo'
```

### Install all the available dependencies from package.JSON

```
npm i
```

### Activate the SASS

```
npm run build
```

## Jest & Cypress

- Run all test dependencies

```
npm run test
```

### Eslint

- Install, fix and format

```
npm run lint
npm run lint-fix
npm run format
```

## Jest

- Run isolated Jest-test

```
npm run test-unit
```

## Cypress E2E

- Run isolated Cypress-test

```
npm run test-e2e
```

## Husky

- commit hooks function

```
git add example.file(s)
git commit -m "test your error"
```
12 changes: 12 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
7 changes: 7 additions & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { defineConfig } = require("cypress");

module.exports = defineConfig({
e2e: {
baseUrl: "http://localhost:8080",
},
});
26 changes: 26 additions & 0 deletions cypress/e2e/3-e2e-testing-ca/loginSpec.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
describe("Login spec", () => {
beforeEach(() => {
cy.visit("/");
cy.wait(1000);
cy.get("#registerForm").within(() => {
cy.get("button[data-bs-target='#loginModal']:visible").click();
});
cy.wait(2000);
});

it("The user can login with the login form with valid credentials", () => {
cy.get("#loginEmail").type("Ramsnes@noroff.no");
cy.get("#loginPassword").type("Marmea88");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real data should be protected in .env and secrets

cy.get("button[type='submit']:visible").click();
cy.get(window.localStorage.getItem("token")).should("not.be.empty");
cy.get(window.localStorage.getItem("profile")).should("not.be.empty");
Comment on lines +15 to +16

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The way you're trying to access localStorage items is incorrect. cy.get is used to get DOM elements, not localStorage items. You should use cy.window().its('localStorage.token') and cy.window().its('localStorage.profile') instead.

-     cy.get(window.localStorage.getItem("token")).should("not.be.empty");
-     cy.get(window.localStorage.getItem("profile")).should("not.be.empty");
+     cy.window().its('localStorage.token').should('exist');
+     cy.window().its('localStorage.profile').should('exist');

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rabbit is telling the truth

});

it("The user cannot submit the login form with invalid credentials and is shown a message", () => {
cy.get("#loginEmail").type("not_real_user@hotmail.no");
cy.get("#loginPassword").type("not_real_password");
cy.get("button[type='submit']:visible").click();
cy.window().its("localStorage.token").should("not.exist");
cy.window().its("localStorage.profile").should("not.exist");
});
});
27 changes: 27 additions & 0 deletions cypress/e2e/3-e2e-testing-ca/logoutSpec.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
describe("Logout spec", () => {
beforeEach(() => {
cy.visit("/");
cy.wait(1000);

cy.get("#registerForm").within(() => {
cy.get("button[data-bs-target='#loginModal']:visible").click();
});
cy.wait(1000);

cy.get("#loginEmail").should("exist");
cy.get("#loginPassword").should("exist");
cy.get("#loginEmail").type("Ramsnes@noroff.no");
cy.get("#loginPassword").type("Marmea88");
cy.wait(2000);
cy.get("button[type='submit']:visible").click();
cy.get(window.localStorage.getItem("token")).should("not.be.empty");
cy.get(window.localStorage.getItem("profile")).should("not.be.empty");
Comment on lines +17 to +18

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

Accessing localStorage directly in Cypress commands is not recommended. Instead, use the cy.window() command to access localStorage. This ensures that you're accessing the same localStorage object as your application.

-    cy.get(window.localStorage.getItem("token")).should("not.be.empty");
-    cy.get(window.localStorage.getItem("profile")).should("not.be.empty");
+    cy.window().its('localStorage.token').should('exist');
+    cy.window().its('localStorage.profile').should('exist');

cy.wait(2000);
});

it("The user can log out with the logout button", () => {
cy.get("button[data-auth='logout']:visible").click();
cy.window().its("localStorage.token").should("not.exist");
cy.window().its("localStorage.profile").should("not.exist");
});
});
5 changes: 5 additions & 0 deletions cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}
25 changes: 25 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
20 changes: 20 additions & 0 deletions cypress/support/e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/e2e.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import "./commands";

// Alternatively you can use CommonJS syntax:
// require('./commands')
Loading
Loading