First to answer your initial question...under normal conditions, test the tank water prior to the water change. Write the numbers in a chart of whatever so you can refer back to them weeks, months, even years later if needed. Testing prior to changing water will show you what is actually occurring biologically. You can test after the W/C too, at least for a few times, to see what changes occurred and then compare that with the next water change tests (prior). Some tests should remain the same, example ammonia and nitrite should always be zero [will come back to the nitrite], nitrate should always be the same or very close and as low as possible, and the pH should remain basically the same every test, within a few decimal points but no more. If nitrate and pH are changing significantly from test to test, something is wrong. Water changes (along with other considerations like stocking, feeding) are intended to maintain stable water chemistry, not make it better every time.
The time of day you test can influence results, with pH. There is a normal diurnal fluctuation, so testing roughly the same time of day will better show if it is consistent (as it should be) or not. For example, if I test in the early morning an hour or so after the tank light comes on, the pH will be at its lowest, say 6.2 as an example. If I test in the late afternoon prior to the light going out, the pH will be higher, say 6.6 or 6.8 maybe. But every morning it will be 6.2 and every evening 6.6/6.8 or very slightly off. That is the consistency you want.
To your nitrite issue...nitrite and nitrate can be connected, one of our members has explained this, but I am not much good with chemistry. But the nitrate at 40 ppm, if tap water is zero, is a concern that must be remedied. Nitrate occurs from biological processes and it can be kept very low by not overstocking, not over feeding, doing regular substantial water changes with substrate vacuuming, keeping the filter very clean, and having live aquatic plants. You want nitrate as close to zero as possible, and you want it to stay there from W/C to W/C. The nitrite may disappear too with this.